如何在单个 azure blob 请求上设置内容处置?

guy*_*rgo 3 video azure content-disposition http-headers azure-storage-blobs

我有一个托管视频的应用程序,我们最近迁移到了 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();
Run Code Online (Sandbox Code Playgroud)

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

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

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

Gau*_*tri 8

然而,在 Azure 上,我似乎必须在我想要的功能之间进行选择,因为必须在文件上而不是在请求上设置内容配置。

有一个解决方法。如您所知Content-Disposition,您可以在 blob 上定义一个属性。但是,当您为此属性定义值时,它将始终应用于该 blob。当您想在 blob 上有选择地应用此属性时(比如在每个请求的基础上),您要做的是Shared Access Signature (SAS)在该 blob 上创建一个并在那里覆盖此请求标头。然后您可以通过 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.
Run Code Online (Sandbox Code Playgroud)

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