c# - 有没有办法强制将 Azure CDN 文件作为浏览器附件下载?

标签 c# asp.net azure cdn azure-cdn

我们正在构建一个与 DAM(数字 Assets 管理)系统集成的 ASP.NET Web 应用程序。 DAM 将文件存储在 Azure Blob 存储中,并使用 Azure CDN 公开提供它们。

我们将提供这些文件(大多数为 PDF)以供从我们的网络应用程序下载。当用户请求这些文件之一时,我们将提供一个自定义 URL,该 URL 将在返回相关文件进行下载之前在服务器上运行一些代码(记录下载等)。

客户端要求文件始终作为浏览器附件返回(即内容处置附件 header )。我很好奇我在这里有什么选择。

我的理想是 CDN URL 是抽象的,而我的自定义 URL 是文件的公共(public) URL。这将允许我设置相关的响应 header 等。但是,我认为这里唯一的解决方案是从 CDN 下载文件并将其缓存在我的 Web 服务器上,这会混淆 CDN 的用途。因此,在完成服务器处理后,我可能必须将客户端重定向到 CDN 公共(public) URL。但是,有没有一种方法可以确保 Azure 返回带有正确响应 header 的文件,以确保委托(delegate)浏览器的默认下载行为?

* 更新 *

在看到这个问题的答案时,我意识到我可能问错了问题。感谢在这里回答的各位。 Follow-up question is here .

最佳答案

TL;DR

您需要在 Blob 存储上配置默认版本,以便它向未经身份验证的客户端显示所需的 header 。 this question中的问题有使其工作的代码。

一旦设置完毕,并且为匿名客户端工作,CDN 将复制所有 header ,并且它应该按预期工作。

设置ContentDisposition

该功能已存在,您可以在 blob property 上设置 ContentDisposition但是,虽然这会在 blob 上设置属性,但它不会传递到 header 。

我使用以下命令通过 Powershell 对此进行了测试(只是因为它比 c# 更快)

$context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey 

$container = Get-AzureStorageContainer -Name $ContainerName -Context $context 

$blobref = ($script:container.CloudBlobContainer.GetBlobReferenceFromServer("images/pier.jpg"))
$blobref.Properties
$blobref.Properties.ContentDisposition = 'attachment; filename="fname.ext"'  
$blobref.SetProperties()  

$blobref = ($script:container.CloudBlobContainer.GetBlobReferenceFromServer("images/pier.jpg"))
$blobref.Properties

产生(除其他外)

ContentDisposition : attachment; filename="fname.ext"

但是,查询 header 时未设置任何内容

([system.Net.HttpWebRequest]::Create($blobref.Uri.AbsoluteUri)).getresponse() 

(回答评论,这些是返回的标题 - 在实验时我也尝试过使用和不使用内容类型 - 因此这里是空白的)

IsMutuallyAuthenticated : False
Cookies                 : {}
Headers                 : {x-ms-request-id, x-ms-version, x-ms-lease-status, x-ms-blob-type...}
SupportsHeaders         : True
ContentLength           : 142224
ContentEncoding         : 
ContentType             : 
CharacterSet            : 
Server                  : Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
LastModified            : 01/03/2016 11:29:04
StatusCode              : OK
StatusDescription       : OK
ProtocolVersion         : 1.1
ResponseUri             : https://xxxx.blob.core.windows.net/cdn/images/pier.jpg
Method                  : GET
IsFromCache             : False

由于 CDN 只会复制 HTTP header 本身的信息,因此该数据不会进入 CDN。

已编辑(经过长时间的评论讨论!)

出于众所周知的原因,Powershell 没有发送 x-ms-version,所以我退回到 telnet,它确实生成了 header -

HEAD  /cdn/images/pier.jpg HTTP/1.1
HOST: xxxx.blob.core.windows.net
x-ms-version: 2015-04-05

HTTP/1.1 200 OK
Content-Length: 142224
Last-Modified: Tue, 01 Mar 2016 11:29:04 GMT
Accept-Ranges: bytes
ETag: "0x8D341C4B1C4F34F"
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: b4f41b01-0001-00d7-7cc9-7384c9000000
x-ms-version: 2015-04-05
x-ms-lease-status: unlocked
x-ms-lease-state: available
x-ms-blob-type: BlockBlob
Content-Disposition: attachment; filename="fname.ext"
Date: Tue, 01 Mar 2016 14:49:17 GMT

关于c# - 有没有办法强制将 Azure CDN 文件作为浏览器附件下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719455/

相关文章:

c# - vs2010循环依赖问题

c# - 在嵌套的强类型转发器中访问父数据

c# - 将现有应用程序移植到 Microsoft Azure 面临哪些挑战?

azure - Microsoft Service Bus 是否会为主题中的每个订阅重复消息?

c# - 随机访问流 Azure block 存储

c# - 添加两个功能?

c# - 使用 XElement select 语句添加多个元素

c# - 来自浏览器的 WCF 方法调用返回 400 错误请求

asp.net - 可以在用户的​​ cookie 中保留 OAuth 访问 token 吗?

azure - 使用消息队列绑定(bind)时如何处理 Azure Function 重新运行?