【问题标题】:Download blob using Azure Blob storage client library v12 for .NET使用适用于 .NET 的 Azure Blob 存储客户端库 v12 下载 Blob
【发布时间】:2020-08-22 11:47:40
【问题描述】:

我正在使用 Azure.Storage.Blobs 版本=12.4.1。 我有一个 REST 端点,我想使用它从存储帐户下载 Blob。

我需要将结果流式传输到 HttpResponseMessage 并且我不想使用 MemoryStream。我想将结果直接流式传输到调用客户端。有没有办法做到这一点。如何在 HttpResponseMessage 内容中获取下载的 blob?我不想使用 MemoryStream,因为会有很多下载请求。

BlobClient 类有一个方法 DownloadToAsync,但它需要一个 Stream 作为参数。

        var result = new HttpResponseMessage(HttpStatusCode.OK);

        var blobClient = container.GetBlobClient(blobPath);
        if (await blobClient.ExistsAsync())
        {
            var blobProperties = await blobClient.GetPropertiesAsync();

            var fileFromStorage = new BlobResponse()
            {                    
                ContentType = blobProperties.Value.ContentType,
                ContentMd5 = blobProperties.Value.ContentHash.ToString(),
                Status = Status.Ok,
                StatusText = "File retrieved from blob"
            };

            await blobClient.DownloadToAsync(/*what to put here*/);
            return fileFromStorage;
        }

【问题讨论】:

    标签: .net azure azure-storage azure-blob-storage azure-sdk


    【解决方案1】:

    您可以简单地创建一个新的内存流并将 blob 的内容下载到该流中。

    类似:

            var connectionString = "UseDevelopmentStorage=true";
            var blobClient = new BlockBlobClient(connectionString, "test", "test.txt");
            var ms = new MemoryStream();
            await blobClient.DownloadToAsync(ms);
    

    ms 将包含 blob 的内容。使用前不要忘记将内存流的位置重置为0

    【讨论】:

    • 我想避免使用 MemoryStream,我不想让 blob 内容在内存中
    • 不清楚你到底想要什么。此方法需要一个流。如果内存流不是一个选项,那么您可能想要保存到文件中?
    • 其他选项是使用返回BlobDownloadInfoDownloadAsync 方法。您可以将该对象直接传递给客户端。
    • DownloadTextAsync 的 v12 替代品是什么?这是加载存储在 blob 存储中的模板的一种非常有用的方法。
    • 嗨,@dinotom,你找到了 DownloadTextAsync 替代品吗?
    【解决方案2】:

    你需要使用

     BlobDownloadInfo download = await blobClient.DownloadAsync();
    

    download.Content 是 blob 流。您可以使用它直接复制到其他流。

    using (var fileStream = File.OpenWrite(@"C:\data\blob.bin"))
    {
        await download.CopyToAsync(fileStream);
    }
    

    【讨论】:

    【解决方案3】:

    尝试使用如下代码将blob下载到HttpResponseMessage中。

    try
    {
        var storageAccount = CloudStorageAccount.Parse("{connection string}");
        var blobClient = storageAccount.CreateCloudBlobClient();
        var Blob = await blobClient.GetBlobReferenceFromServerAsync(new Uri("https://{storageaccount}.blob.core.windows.net/{mycontainer}/{blobname.txt}"));
        var isExist = await Blob.ExistsAsync();
        if (!isExist) {
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "file not found");
        }
        HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);
        Stream blobStream = await Blob.OpenReadAsync();
        message.Content = new StreamContent(blobStream);
        message.Content.Headers.ContentLength = Blob.Properties.Length;
        message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(Blob.Properties.ContentType);
        message.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
        {
            FileName = "{blobname.txt}",
            Size = Blob.Properties.Length
        };
        return message;
    }
    catch (Exception ex)
    {
        return new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.InternalServerError,
            Content = new StringContent(ex.Message)
        };
    }
    

    【讨论】:

    • 这是我以前有的,但现在我需要使用Azure.Storage.Blobs version=12.4.1和没有OpenReadAsync的BlobClient
    • 同样的问题。如果这反映了 v12 的最新 Azure.Storage.Blogs 库,那就太好了。
    猜你喜欢
    • 2022-01-07
    • 2021-05-21
    • 2022-01-27
    • 2019-03-21
    • 2021-01-04
    • 2021-02-15
    • 2022-12-15
    • 1970-01-01
    • 2016-06-13
    相关资源
    最近更新 更多