gpt4 book ai didi

azure - 如何设置单个 azure blob 请求的内容处置?

转载 作者:行者123 更新时间:2023-12-02 09:17:04 25 4
gpt4 key购买 nike

我有一个托管视频的应用程序,我们最近迁移到了 Azure。

在我们的旧应用程序中,我们为用户提供了播放或下载视频的能力。然而,在 Azure 上,我似乎必须在我想要的功能之间进行选择,因为内容配置必须在文件上设置,而不是在请求上设置。

到目前为止,我已经想出了两个非常糟糕的解决方案。

第一个解决方案是通过我的 MVC 服务器流式传输下载。

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("videos");
string userFileName = service.FirstName + service.LastName + "Video.mp4";
Response.AddHeader("Content-Disposition", "attachment; filename=" + userFileName); // force download
container.GetBlobReference(service.Video.ConvertedFilePath).DownloadToStream(Response.OutputStream);
return new EmptyResult();

此选项适用于较小的视频,但对我的服务器来说非常繁重。对于较大的视频,操作会超时。

第二个选项是将每个视频托管两次。

这个选项显然很糟糕,因为我将不得不支付双倍的存储费用。

最佳答案

However on Azure it seems like I have to pick between which functionality I want, as the content disposition has to be set on the file and not on the request.

有一个解决方法。您可能知道,您可以在 blob 上定义一个 Content-Disposition 属性。但是,当您为此属性定义值时,它将始终应用于该 blob。当您想要有选择地在 blob 上应用此属性(例如基于每个请求)时,您要做的就是在该 blob 上创建一个共享访问签名 (SAS) 并在那里覆盖此请求 header 。然后您可以通过 SAS URL 提供 blob。

这是示例代码:

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("videos");
string userFileName = service.FirstName + service.LastName + "Video.mp4";
CloudBlockBlob blob = container.GetBlockBlobReference(userFileName);
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1)
};
SharedAccessBlobHeaders blobHeaders = new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=" + userFileName
};
string sasToken = blob.GetSharedAccessSignature(policy, blobHeaders);
var sasUrl = blob.Uri.AbsoluteUri + sasToken;//This is the URL you will use. It will force the user to download the video.

我很久以前写了一篇博客文章,您可能会觉得有用:http://gauravmantri.com/2013/11/28/new-changes-to-windows-azure-storage-a-perfect-thanksgiving-gift/

关于azure - 如何设置单个 azure blob 请求的内容处置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45933679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com