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

标签 azure video http-headers azure-blob-storage content-disposition

我有一个托管视频的应用程序,我们最近迁移到了 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/47571581/

相关文章:

http - 如何在Rebol3中获取HTTP响应头?

c# - Xamarin Native 中 AcquireTokenAsync 和 LoginAsync 之间的差异

azure - 将文件上传到 Azure webrole 应用程序目录

video - 我正在使用以下代码将 YouTube 视频转换为音频,但尽管安装了 ffmpeg,但仍出现错误

rest - 是否有 READ-ONLY HTTP 响应 header ?

asp-classic - 如何在经典 ASP 中使用 HTTP 子状态发送 HTTP 错误代码?

azure - 我可以使用哪些 Microsoft Graph 用户属性来获取每个用户的附加信息

Azure PowerShell Cmdlet 和设置服务诊断 (Set-AzureServiceDiagnosticsExtension)

google-chrome - 普通视频无法在Firefox中播放(YouTube视频可以正常播放)

audio - 计算 PTS 和 DTS 以在 ffmpeg 中编码新的视频和音频