【问题标题】:Download a file from Azure Storage using Azure.Storage.Blobs .NET使用 Azure.Storage.Blobs .NET 从 Azure 存储下载文件
【发布时间】:2022-12-24 11:59:48
【问题描述】:

我正在使用 NuGet 包Azure.Storage.Blobs v.12.14.1从 Azure 存储下载文件,然后将文件上传到 OneDrive。

这是我正在使用的代码:

var blobClient = new BlobClient({connection_string}, {container}, {file_path});
var blobSize = blobClient.GetProperties().Value.ContentLength;
var client = _clientFactory.CreateClient();

long blockSize = (10 * 1024 * 1024);
blockSize = Math.Min(blobSize, blockSize);

long currentPointer = 0;
long bytesRemaining = blobSize;
do
{
    var bytesToFetch = Math.Min(blockSize, bytesRemaining);
    await using (var blobStream = await blobClient.OpenReadAsync(currentPointer, (int)bytesToFetch, null, cancellationToken))
    {
        blobStream.Position = 0;

        var fileContent = new StreamContent(blobStream);
        fileContent.Headers.ContentLength = bytesToFetch;
        fileContent.Headers.ContentRange = new ContentRangeHeaderValue(currentPointer, currentPointer + bytesToFetch - 1, blobSize);

        var res = await client.PutAsync(fileConsentCardResponse.UploadInfo.UploadUrl, fileContent, cancellationToken);

        currentPointer += bytesToFetch;
        bytesRemaining -= bytesToFetch;
    }
}
while (bytesRemaining > 0);

当文件小于 blockSize: 10MB 时,这工作正常。代码执行后可以在OneDrive中看到该文件。

但是,当文件大于 blockSize 时,这将不再有效。不会引发任何错误,但在 OneDrive 中找不到该文件。

请帮忙,谢谢!!

更新:

我做了这个工作:

var blobClient = new BlobClient({connection_string}, {container}, {file_path});
var blobSize = blobClient.GetProperties().Value.ContentLength;
var client = _clientFactory.CreateClient();

long blockSize = (10 * 1024 * 1024);
blockSize = Math.Min(blobSize, blockSize);

long currentPointer = 0;
long bytesRemaining = blobSize;
do
{
    var bytesToFetch = Math.Min(blockSize, bytesRemaining);
    HttpRange range = new HttpRange(currentPointer, bytesToFetch);

    var blobStreamResult = await blobClient.DownloadStreamingAsync(
        new BlobDownloadOptions { Range = range });

    using (MemoryStream ms = new MemoryStream())
    {
        blobStreamResult.Value.Content.CopyTo(ms);
        ms.Position = 0;
        var fileContent = new StreamContent(ms);

        fileContent.Headers.ContentLength = bytesToFetch;
        fileContent.Headers.ContentRange = new ContentRangeHeaderValue(currentPointer, currentPointer + bytesToFetch - 1, blobSize);

        var res = await client.PutAsync(fileConsentCardResponse.UploadInfo.UploadUrl, fileContent, cancellationToken);
    }

    currentPointer += bytesToFetch;
    bytesRemaining -= bytesToFetch;
}
while (bytesRemaining > 0);

还有一个问题,如果文件很大(比如1GB),它会消耗太多内存,如何解决这个问题?谢谢!

【问题讨论】:

    标签: .net download upload azure-blob-storage azure-storage


    【解决方案1】:

    对于 OpenRead 场景: 我对您的代码 sn-p 有几个问题,因为其中一些方法是由您制作的,即:

    var client = _clientFactory.CreateClient();
    var res = await client.PutAsync(fileConsentCardResponse.UploadInfo.UploadUrl, fileContent, cancellationToken);

    这些 API 都不属于 Azure.Storage.Blobs,您能提供它的代码 sn-p 吗? 我已经尝试了一个缓冲区大小为 10MB 的基本场景,但是我无法重现您的问题。所以我们必须分析,你究竟是如何从 OpenRead Stream 读取的。

    对于 DownloadStreaming 场景: 看起来可能会将整个流缓冲两次(一次是在执行 CopyTo 时,一次是在初始化 StreamContent 时)。这听起来有点超出我们审查客户代码的带宽。

    但我建议在检索到 DownloadStreaming 的结果内容后对其进行处理,并缓冲部分流并将其传递给 StreamContent,而不是尝试一次完成所有操作。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2021-07-15
      • 2021-12-08
      • 2015-06-20
      • 2012-02-04
      • 2018-01-09
      • 2020-11-09
      相关资源
      最近更新 更多