【问题标题】:Is there a way to get a blob's ContentType in Azure.Storage.Blobs?有没有办法在 Azure.Storage.Blobs 中获取 blob 的 ContentType?
【发布时间】:2021-03-10 16:55:08
【问题描述】:

我正在切换到较新的 Azure.Storage.Blobs,并希望通过 API 将 Blob 作为文件提供给客户端。

在现已弃用的 Microsoft.Azure.Storage 和 Microsoft.Azure.Storage.Blob 中,我可以这样做:

using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;

    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = cloudBlobClient.GetContainerReference("foo");
    CloudBlockBlob blob = container.GetBlockBlobReference(blobId);
    var bar = blob.Properties.ContentType;

现在我在 Azure.Storage.Blobs 中,设置看起来相同,但我缺少关键点:

using Azure.Storage.Blobs;
    BlobContainerClient containerClient = new BlobContainerClient(storageConnectionString, containerName);
    BlobClient blobClient = containerClient.GetBlobClient("foo");

较新的库中没有类似的块blob,我可以将 blobClient 读取到流中,但是在不求助于旧库的情况下,我从哪里获得它的内容类型?

我希望能够点击 FileResult,返回与此签名匹配的内容:

File(Stream fileStream, string contentType, string fileName);

【问题讨论】:

    标签: c# azure azure-blob-storage


    【解决方案1】:

    您可以使用 GetBlobProperties 获取 blob 的属性,这将为您提供 blob 的内容类型。类似(未经测试的代码):

    var getBlobPropertiesResult = blobClient.GetProperties();
    var contentType = getBlobPropertiesResult.Value.ContentType;
    

    【讨论】:

    • 这对我来说是个错误——响应 不包含“ContentType”的定义。如果我应该添加一个参考,我应该看哪一个?
    • 很抱歉。我现在不在电脑前,但你能试试 blobProperties.Value.ContentType. 吗?
    • 天哪,我不敢相信我没有那样做。我需要一杯咖啡。或者打个盹,或者两者兼而有之。非常感谢
    • 我很高兴它为你解决了:)。也更新了我的答案。
    猜你喜欢
    • 2020-02-17
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    相关资源
    最近更新 更多