您可以参考使用相同框架库的代码:
首先,包括你需要的类,这里我们包括存储客户端库、存储数据移动库和.NET线程,因为数据移动库提供了任务异步接口来传输存储对象:
using System;
using System.Threading;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.DataMovement;
现在使用 Storage 客户端 lib 提供的接口来设置存储上下文(了解如何从 .NET 使用 Blob 存储的更多详细信息):
string storageConnectionString = "myStorageConnectionString";
CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("mycontainer");
blobContainer.CreateIfNotExists();
string sourcePath = "path\\to\\test.txt";
CloudBlockBlob destBlob = blobContainer.GetBlockBlobReference("myblob");
设置存储 blob 上下文后,您可以开始使用 WindowsAzure.Storage.DataMovement.TransferManager 上传 blob 并跟踪上传进度,
// Setup the number of the concurrent operations
TransferManager.Configurations.ParallelOperations = 64;
// Setup the transfer context and track the upoload progress
SingleTransferContext context = new SingleTransferContext();
context.ProgressHandler = new Progress<TransferStatus>((progress) =>
{
Console.WriteLine("Bytes uploaded: {0}", progress.BytesTransferred);
});
// Upload a local blob
var task = TransferManager.UploadAsync(
sourcePath, destBlob, null, context, CancellationToken.None);
task.Wait();
了解更多:
Develop for Azure File storage with .Net
Storage Client Library Reference for .NET - MSDN
如果要将 blob 复制到文件或将文件复制到 blob,则必须使用共享访问签名 (SAS) 对源对象进行身份验证,即使是在同一存储帐户中复制也是如此。