- Microsoft Windows Azure Development Cookbook
- Neil Mackenzie
- 551字
- 2021-04-02 18:51:59
Downloading a blob asynchronously
The Windows Azure Storage Client library provides synchronous and asynchronous versions of nearly all the methods that access the Windows Azure Storage Service.
The asynchronous methods follow the common language runtime (CLR) Asynchronous Programming Model (APM). In this model, asynchronous methods for an action are defined as a pair named BeginAction
and EndAction
. The asynchronous operation is initiated through a call to BeginAction
and is cleaned up by a call to EndAction
. BeginAction
has a parameter that is a callback delegate and EndAction
must be invoked in that delegate.
This apparent complexity can be greatly simplified through the use of a lambda expression to represent the callback delegate. Furthermore, local variables defined in the method containing the lambda expression are available inside the lambda expression. This removes any difficulty caused by a need to pass variables into the delegate. Using a lambda expression, instead of a callback delegate, makes using the asynchronous methods almost as simple as using the synchronous methods.
In this recipe, we will learn how to use asynchronous methods to download a blob into a file.
How to do it...
We are going to download a blob asynchronously. We do this as follows:
- Add a new class named
DownloadBlobExample
to the project. - Set the Target Framework for the project to .NET Framework 4.
- Add the following assembly references to the project:
Microsoft.WindowsAzure.StorageClient System.Configuration
- Add the following
using
statements to the top of the class file:using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient; using System.Configuration; using System.Net; using System.IO;
- Add the following method, creating a container and blob, to the class:
private static void CreateContainerAndBlob(String containerName, String blobName) { CloudStorageAccount cloudStorageAccount =CloudStorageAccount.Parse(ConfigurationManager.AppSettings[ "DataConnectionString"]); CloudBlobClient cloudBlobClient =cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer cloudBlobContainer =cloudBlobClient.GetContainerReference(containerName); cloudBlobContainer.CreateIfNotExist(); CloudBlockBlob cloudBlockBlob =cloudBlobContainer.GetBlockBlobReference(blobName); cloudBlockBlob.UploadText("To be, or not to be"); }
- Add the following method, downloading a blob, to the class:
private static void DownloadBlob(String containerName,String blobName, String fileName) { CloudStorageAccount cloudStorageAccount =CloudStorageAccount.Parse(ConfigurationManager.AppSettings["DataConnectionString"]); CloudBlobClient cloudBlobClient =cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer cloudBlobContainer =cloudBlobClient.GetContainerReference(containerName); CloudBlockBlob cloudBlockBlob =cloudBlobContainer.GetBlockBlobReference(blobName); FileStream fileStream = new FileStream(fileName,FileMode.Append); IAsyncResult iAsyncResult =cloudBlockBlob.BeginDownloadToStream(fileStream,(result) => { cloudBlockBlob.EndDownloadToStream(result); fileStream.Close(); }, null); return; }
- Add the following method, using the methods added earlier, to the class:
public static void UseDownloadBlobExample() { String containerName = "{CONTAINER_NAME}"; String blobName = "{BLOB_NAME}"; String fileName = Path.GetTempPath() + @"\{FILE_NAME}"; CreateContainerAndBlob(containerName, blobName); DownloadBlob(containerName, blobName, fileName); }
- Add the following to the
configuration
section ofapp.config
:<appSettings> <add key="DataConnectionString"value="UseDevelopmentStorage=true"/> </appSettings>
How it works...
In steps 1 through 4, we set up the class. In step 5, we initialize a CloudStorageAccount
from app.config
and use it to create a CloudBlobClient
instance. We use this to create a container and a blob.
In step 6, we follow the same route to get a CloudBlockBlob
reference to the blob. We use it to invoke the BeginDownloadToStream()
method. We pass this a lambda expression, instead of a callback delegate, and invoke EndDownloadToStream()
in the lambda expression to clean up the asynchronous call. We use the ability to refer to local objects from inside the lambda expression to close the FileStream
used in the download.
In step 7, we add a method that sets the parameters for the call to DownloadBlob()
. We download the blob to a temp
directory. We must replace {CONTAINER_NAME}
and {BLOB_NAME}
with appropriate container and blob names, and {FILE_NAME}
with the file name.
In step 8, we add the connection string to the app.config
configuration file.
- 數(shù)碼攝影后期零基礎(chǔ)入門教程
- 中文版SolidWorks 2015技術(shù)大全
- Inkscape 0.48 Illustrator's Cookbook
- 中文版Photoshop 2020完全自學(xué)教程
- Google App Engine Java and GWT Application Development
- Midjourney商業(yè)設(shè)計(jì)完全教程
- 中文版Corel DRAW X5案例實(shí)訓(xùn)教材
- 中文版CATIA V5 技術(shù)大全
- AI賦能Rhino產(chǎn)品造型創(chuàng)意設(shè)計(jì)(視頻教學(xué)版)
- 中文版Flash CC動(dòng)畫制作實(shí)用教程
- After Effects 動(dòng)態(tài)圖形設(shè)計(jì):入門技法與基礎(chǔ)創(chuàng)作
- Photoshop移動(dòng)UI設(shè)計(jì)
- Excel+Python:飛速搞定數(shù)據(jù)分析與處理
- 設(shè)計(jì)+制作+印刷+商業(yè)模版:InDesign實(shí)例教程
- 精通Microsoft 365云計(jì)算管理:SharePoint Online篇