【问题标题】:Azure Storage Blob Download Progress IndicatorAzure 存储 Blob 下载进度指示器
【发布时间】:2021-02-17 05:06:05
【问题描述】:

下载 blob 时我需要一个进度指示器。

上传时进度指示器已经工作。我在 BlobUploadOptions 中使用 Progresshandler。

BlobDownloadDetails 似乎有进度状态。但是,我不知道如何集成它以使其工作。

这是我的代码:

IKeyEncryptionKey key;
IKeyEncryptionKeyResolver keyResolver;

// Create the encryption options to be used for upload and download.
ClientSideEncryptionOptions encryptionOptions = new ClientSideEncryptionOptions(ClientSideEncryptionVersion.V1_0)
{
   KeyEncryptionKey = key,
   KeyResolver = keyResolver,
   // string the storage client will use when calling IKeyEncryptionKey.WrapKey()
   KeyWrapAlgorithm = "some algorithm name"
};

// Set the encryption options on the client options
BlobClientOptions options = new SpecializedBlobClientOptions() { ClientSideEncryption = encryptionOptions };

// Get your blob client with client-side encryption enabled.
// Client-side encryption options are passed from service to container clients, and container to blob clients.
// Attempting to construct a BlockBlobClient, PageBlobClient, or AppendBlobClient from a BlobContainerClient
// with client-side encryption options present will throw, as this functionality is only supported with BlobClient.
BlobClient blob = new BlobServiceClient(connectionString, options).GetBlobContainerClient("myContainer").GetBlobClient("myBlob");

        BlobUploadOptions uploadOptions = new BlobUploadOptions();
        uploadOptions.ProgressHandler = new Progress<long>(percent =>
        {
           progressbar.Maximum = 100;
           progressbar.Value = Convert.ToInt32(percent * 100 / file.Length);
        });

// Upload the encrypted contents to the blob.
blob.UploadAsync(content: stream, options: uploadOptions, cancellationToken: CancellationToken.None);

// Download and decrypt the encrypted contents from the blob.
MemoryStream outputStream = new MemoryStream();
blob.DownloadTo(outputStream);

【问题讨论】:

    标签: c# .net azure azure-blob-storage azure-keyvault


    【解决方案1】:

    目前,DownloadTo 方法不支持进度监视器。查看源码时,DownloadTo 方法定义在BlobBaseClient 类中,而UploadAsync 方法定义在BlobClient 类中,而BlobClient 类继承自BlobBaseClient 类。所以我认为他们可能会错过基类BlobBaseClient中的这个功能。

    但是有一个变通方法,代码如下:

    class Program
    {
        static void Main(string[] args)
        {
            var connectionString = "DefaultEndpointsProtocol=https;AccountName=xx;AccountKey=xxx;EndpointSuffix=core.windows.net";
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient("xxx");
            BlobClient blobClient = blobContainerClient.GetBlobClient("xxx");
    
            var blobToDownload = blobClient.Download().Value;
            
            MemoryStream outputStream = new MemoryStream();
    
            var downloadBuffer = new byte[81920];
            int bytesRead;
            int totalBytesDownloaded = 0;
    
            while ((bytesRead = blobToDownload.Content.Read(downloadBuffer, 0, downloadBuffer.Length)) != 0)
            {
                outputStream.Write(downloadBuffer, 0, bytesRead);
                totalBytesDownloaded += bytesRead;
                Console.WriteLine(GetProgressPercentage(blobToDownload.ContentLength, totalBytesDownloaded));
            }
    
            Console.WriteLine("**completed**");
            Console.ReadLine();
        }
    
        private static double GetProgressPercentage(double totalSize, double currentSize)
        {
            return (currentSize / totalSize) * 100;
        }
    }
    

    这里是reference doc

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 2022-01-26
      • 1970-01-01
      • 2015-04-24
      • 2019-12-19
      • 2019-09-05
      • 2016-03-24
      • 2020-10-14
      • 2021-12-08
      相关资源
      最近更新 更多