azure - 为什么使用自定义文件扩展名上传到 azure blob 容器的 gzip 压缩文件会通过生成的 sas 下载为 .gz?

标签 azure .net-core azure-blob-storage gzipstream

这是我生成 SAS 的方法:

        private string GenerateSasBlob(BlobClient blobClient)
        {
            BlobSasBuilder sasBuilder = new BlobSasBuilder()
            {
                BlobContainerName = blobClient.GetParentBlobContainerClient().Name,
                BlobName = blobClient.Name,
                Resource = "b",
                StartsOn = DateTimeOffset.UtcNow,
                ExpiresOn = DateTimeOffset.UtcNow.AddMonths(1),
                Protocol = SasProtocol.Https,
                ContentType = "mygzip"
            };
            sasBuilder.SetPermissions(BlobSasPermissions.Read);

            return blobClient.GenerateSasUri(sasBuilder).ToString();
        }

我认为通过指定 ContentType 可以修复它,但是生成的 sas 仍然下载为 .gz,而不是预期的 .mygzip。

虽然并排查看压缩文件内的内容是相同的,但我需要的是下载时它应该是 .mygzip 。知道如何做吗?

最佳答案

考虑到您希望使用不同的扩展名(mygzip 而不是 gz)下载 blob,本质上您会希望使用不同的名称下载 blob。

在这种情况下,您想要覆盖的响应 header 是 Content-Disposition而不是 Content-Type

您的代码将类似于:

private string GenerateSasBlob(BlobClient blobClient)
{
    var newBlobName = blobClient.Name.Replace(".gz", ".mygzip");//Change the extension here in the name
    BlobSasBuilder sasBuilder = new BlobSasBuilder()
    {
        BlobContainerName = blobClient.GetParentBlobContainerClient().Name,
        BlobName = blobClient.Name,
        Resource = "b",
        StartsOn = DateTimeOffset.UtcNow,
        ExpiresOn = DateTimeOffset.UtcNow.AddMonths(1),
        Protocol = SasProtocol.Https,
        ContentDisposition = $"attachment; filename=\"{newBlobName}\""
    };
    sasBuilder.SetPermissions(BlobSasPermissions.Read);

    return blobClient.GenerateSasUri(sasBuilder).ToString();
}

现在,当用户单击 SAS URL 时,将下载带有“mygzip”扩展名的 blob。

有关 Content-Disposition header 的更多信息可以在此处找到:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition .

关于azure - 为什么使用自定义文件扩展名上传到 azure blob 容器的 gzip 压缩文件会通过生成的 sas 下载为 .gz?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71477487/

相关文章:

azure - 批量查询异步复制 Blob Azure API 的状态

c# - .net core 1.1 entityframework与mysql数据库连接

c# - SynchronizationContext 不再与 ExecutionContext 一起流动(从 .NET Framework 到 .NET Core)?

azure - 尝试在 IoT 中心设置文件上传时找不到存储帐户

c# - 如何以编程方式将 Azure WebJob 标记为失败?

azure - Core 2.2 上的 SignalR 部署到 Azure 时失败

c# - 如何使用 ListBlobsSegmentedAsync 从 Azure BLOB 中的目录获取所有文件

c# - 以编程方式从 Azure Blob 存储连接字符串中提取属性

azure - 在 Azure Web 角色上部署 PDB

azure - 是否可以命名 Azure 网络观察程序?