Azure Blob Storage V2不再提供content-disposition头信息

8
我有一个关于Azure Blob Storage V2的“content-disposition” blob属性的问题。
我使用Azure Storage Explorer 1.6.2 (见截图) 和 Azure Portal 配置了文件 howto-201901.pdf 的属性为 "attachment; filename=howto.pdf"。虽然该属性设置在文件上,但在下载时却未作为头信息传递。
早期的存储 V1 中没有这个问题。如果我下载文件 howto-2010901.pdf,HTTP头中的content-disposition会被设置,浏览器会按照我的配置下载文件 howto-pdf。
但是自从两三个月前(可能是我的存储升级到V2后)就没有这个功能了。浏览器会使用原始名称下载文件。
有人可以提供解决此行为的信息吗?
最好的Tino

enter image description here


1
我找到了这种行为的原因和解决方案。 我猜测如果URL没有SAS令牌,那么content-disposition HTTP头信息将不再设置。 添加SAS令牌,问题就解决了。https://learn.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1 - user2209131
4个回答

5
当下载URL未经身份验证时,响应中的Content-Disposition头部未被发送给客户端。
为了让客户端接收到 Content-Disposition,需要执行以下步骤:
  1. 创建一个具有有限访问权限的SAS令牌。
  2. 将其附加到Blob下载链接上。

你知道这在文档中有没有说明吗?还是通过试错找到的? - Matt
通过大量的尝试和错误 @Matt - Vikas
通过大量的尝试和错误 @Matt - undefined
1
将其附加到Blob下载链接。这句话的确切含义是什么? - Mattwmaster58
@Mattwmaster58 我记不太清楚,但我想我是想要添加一个URL参数。 - Vikas

1
我遇到了同样的问题,但是我对.NET SDK和SharedAccessBlobPolicy感到非常困惑。
我正在使用版本为12.4.1Azure.Storage.Blobs SDK来管理存储。是否可以使用它来设置SharedAccessBlobPolicy,还是我应该采用不同的方法?我试图查看文档,但它并没有真正提供帮助,我只能找到关于被认为已经过时的Microsoft.Azure.Storage.Blob SDK版本11的信息。

1
这是一个可能的解决方案,对我有用。
不需要创建新策略,您也可以从 Blob 存储中获取现有策略。请参见 https://learn.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1
    private Uri GetDownloadUri(CloudBlockBlob blob)
    {
        try
        {
            // Return the SAS token.
            var query = GenerateSASQueryString(blob);

            UriBuilder newUri = new UriBuilder(blob.Uri)
            {
                Query = query
            };

            return newUri.Uri;
        }
        catch (UriFormatException ex)
        {
            Console.WriteLine(ex);
        }

        return blob.Uri;
    }

    private string GenerateSASQueryString(CloudBlockBlob blob)
    {
        if (blob == null)
            return null;

        // Create a new access policy for the account.
        SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy
        {
            Permissions = SharedAccessBlobPermissions.Read,
            SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddHours(24),
            SharedAccessStartTime = DateTimeOffset.UtcNow
        };

        // Return the SAS token.
        var query = blob.GetSharedAccessSignature(policy);
        return query;
    }

0
这对我来说修复了缺失的内容分发头,而不需要共享访问签名。
// Only need to run this code once for the storage account.
Azure.Storage.Blobs.BlobServiceClient client = ...

var properties = client.GetProperties().Value;
properties.DefaultServiceVersion = "2023-08-03"; // Latest service version at time of writing.
client.SetProperties(properties);

信用:https://www.devtrends.co.uk/blog/fixing-azure-blob-storage-content-disposition

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接