C# Azure存储Blob下载进度指示器

C# Azure存储Blob下载进度指示器,c#,.net,azure,azure-storage-blobs,azure-keyvault,C#,.net,Azure,Azure Storage Blobs,Azure Keyvault,下载blob时,我需要一个进度指示器 上载时,进度指示器已起作用。我正在BlobUploadOptions中使用Progresshandler BlobDownloadDetails似乎处于进度状态。然而,我不知道如何整合它使其工作 这是我的密码: IKeyEncryptionKey key; IKeyEncryptionKeyResolver keyResolver; // Create the encryption options to be used for upload and dow

下载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);
IKeyEncryptionKey;
IKeyEncryptionKeyResolver钥匙分解器;
//创建用于上载和下载的加密选项。
ClientSideEncryptionOptions encryptionOptions=新的ClientSideEncryptionOptions(ClientSideEncryptionVersion.V1\u 0)
{
KeyEncryptionKey=密钥,
KeyResolver=KeyResolver,
//调用IKeyEncryptionKey.WrapKey()时存储客户端将使用的字符串
KeyWrapAlgorithm=“某些算法名称”
};
//在客户端选项上设置加密选项
BlobClientOptions=new SpecializedBlobClientOptions(){ClientSideEncryption=encryptionOptions};
//获取启用客户端加密的blob客户端。
//客户端加密选项从服务传递到容器客户端,容器传递到blob客户端。
//试图从BlobContainerClient构造BlockBlobClient、PageBlobClient或AppendBlobClient
//如果存在客户端加密选项,则将抛出,因为只有BlobClient才支持此功能。
BlobClient blob=新的BlobServiceClient(connectionString,options)。GetBlobContainerClient(“myContainer”)。GetBlobClient(“myBlob”);
BlobUploadOptions uploadOptions=新建BlobUploadOptions();
uploadOptions.ProgressHandler=新进度(百分比=>
{
最大进度条=100;
progressbar.Value=Convert.ToInt32(百分比*100/文件长度);
});
//将加密内容上载到blob。
UploadAsync(内容:流,选项:uploadOptions,cancellationToken:cancellationToken.None);
//从blob下载并解密加密内容。
MemoryStream outputStream=新的MemoryStream();
blob.DownloadTo(outputStream);

目前,
DownloadTo
方法不支持进度监视器。浏览源代码时,
BlobBaseClient
类中定义了
DownloadTo
方法,而
UploadAsync
方法是在继承自
BlobBaseClient
类的
BlobClient
类中定义的。因此,我认为他们可能会在基类
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;
    }
}
这是你的电话号码