首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure Storage下载进度指示符

Azure Storage下载进度指示符
EN

Stack Overflow用户
提问于 2020-11-04 13:38:32
回答 2查看 1.8K关注 0票数 0

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

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

BlobDownloadDetails似乎有一个进步的状态。但是,我不知道如何将其集成以使其工作。

这是我的代码:

代码语言:javascript
复制
IKeyEncryptionKey key;
IKeyEncryptionKeyResolver keyResolver;

// Create the encryption options to be used for upload and download.
ClientSideEncryptionOptions encryptionOptions = new ClientSideEncryptionOptions(ClientSideEncryptionVersion.V2_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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-05 06:25:27

目前,DownloadTo方法不支持进度监视器。当遍历源代码时,DownloadTo方法是在BlobBaseClient类中定义的,而UploadAsync方法是在继承自BlobBaseClient类的BlobClient类中定义的。因此,我认为他们可能忽略了基类BlobBaseClient中的这个特性。

但是有一个解决办法,代码如下:

代码语言:javascript
复制
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;
    }
}

这是参考文档

票数 1
EN

Stack Overflow用户

发布于 2022-11-04 11:29:58

DownloadStreamingAsync支持进度回调,因此这里有另一种方法..。

代码语言:javascript
复制
{
    ...
    var blobClient = containerClient.GetBlobClient(fileName);
    var targetPath = Path.Combine(Path.GetTempPath(), fileName);
    var prop = await blobClient.GetPropertiesAsync();
    var length = prop.Value.ContentLength;
    var download = await blobClient.DownloadStreamingAsync(new HttpRange(0, length),
        new BlobRequestConditions(),
        false,
        new ProgressTracker(log, length),
        CancellationToken.None);

    var data = download.Value.Content;

    await using (var fileStream = File.Create(targetPath))
    {
        await data.CopyToAsync(fileStream);
    }
}

private class ProgressTracker : IProgress<long>
{
    private readonly TextWriter _logWriter;
    private readonly long _totalLength;
    private double _progress = -100;

    public ProgressTracker(TextWriter logWriter, long totalLength)
    {
        _totalLength = totalLength;
        _logWriter = logWriter;
    }

    public void Report(long value)
    {
        var progress = ((double)value / _totalLength) * 100;
        if (Math.Abs(progress - _progress) > 1)
        {
            _progress = progress;
            _logWriter.WriteLine($"{Math.Round(_progress, 0)}%");
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64681168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档