这是我生成 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/