微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Azure存储Blob下载进度指示器

如何解决Azure存储Blob下载进度指示器

下载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);

解决方法

当前,DownloadTo方法不支持进度监视器。浏览源代码时,DownloadTo类中定义了BlobBaseClient方法,而继承自UploadAsync类的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,downloadBuffer.Length)) != 0)
        {
            outputStream.Write(downloadBuffer,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。