如何在单个 azure blob 请求上设置内容处置? [英] How to set content disposition on individual azure blob requests?

查看:33
本文介绍了如何在单个 azure blob 请求上设置内容处置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an application that hosts videos, and we recently migrated to Azure.

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

On our old application we gave the ability for users to either play or download the video. 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.

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

So far I have came up with two very poor solutions.

第一个解决方案是通过我的 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();

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

This option works okay for smaller videos, but it is very taxing on my server. For larger videos the operation times out.

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

这个选项显然不好,因为我将不得不支付双倍的存储成本.

This option is obviously bad, as I will have to pay double the storage cost.

推荐答案

但是在 Azure 上,我似乎必须在其中进行选择我想要的功能,因为必须在文件而不是请求.

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 上创建一个 Shared Access Signature (SAS) 并在那里覆盖此请求标头.然后您可以通过 SAS URL 提供 blob.

There's a workaround for that. As you may know there's a Content-Disposition property that you can define on a blob. However when you define a value for this property, it will always be applied on that blob. When you want to selectively apply this property on a blob (say on a per request basis), what you do is create a Shared Access Signature (SAS) on that blob and override this request header there. Then you can serve the blob via SAS URL.

这是用于此的示例代码:

Here's the sample code for this:

        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/.

I wrote a blog post about the same long time ago that you may find useful: http://gauravmantri.com/2013/11/28/new-changes-to-windows-azure-storage-a-perfect-thanksgiving-gift/.

这篇关于如何在单个 azure blob 请求上设置内容处置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆