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

查看:268
本文介绍了如何在单个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上创建共享访问签名(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.

以下是这个示例代码:

        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 /十一分之二千零一十三/ 28 /新变化到Windows-Azure的存储一个完美的感恩大礼/

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天全站免登陆