Is there an approach to force a Azure CDN file to download as a browser attachment?

独自空忆成欢 提交于 2019-12-12 18:08:49

问题


We're building an ASP.NET web application which integrates with a DAM (digital asset management) system. The DAM stores files in Azure blob storage and makes them available publicly using Azure CDN.

We will be making these files (most are PDFs) available to download from our web app. When a user requests one of these files we will provide a custom URL which will run some code on the server (logging the download etc) before returning the relevant file for download.

The client requires that the file is always returned as a browser attachment (i.e. content disposition attachment header). I am curious about what options I have here.

My ideal would be that the CDN URL is abstracted and my custom URL is the public URL for the file. That would allow me to set relevant response headers etc. However, I assume the only solution here would be to download the file from CDN and cache it on my web server which would obfuscate the CDN's purpose. So presumably I have to redirect the client to the CDN public URL once I've done my server processing. But then is there a way I can ensure the file is returned by Azure with the correct response headers to ensure the browser's default download behaviour is delegated?

* Update *

In seeing the answers to this question I realised I was perhaps asking the wrong question. Thank you to those of you who answered here. Follow-up question is here.


回答1:


TL;DR

You need to configure the default version on the blob storage in order for it to show the required header to non-authenticated clients. the question in this question has the code to make it work.

Once this is set, and working for anonymous clients the CDN will copy all of the headers across and it should work as expected.

Setting ContentDisposition

The functionality is present, you can set ContentDisposition on a blob property However, while this will set the property on the blob, it does not pass through to the header.

I tested this with Powershell using the following (just because its quicker than 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

Which produces (amongst others)

ContentDisposition : attachment; filename="fname.ext"

However nothing is set when the headers are queried

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

(to answer comment,. these are the headers returned - while experimenting I also tried with and without a contenttype - hence it being blank here)

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

And since CDN will only copy the information from the HTTP headers themselves, this data isn't making it into CDN.

edited (after extended comment chatter!)

For reasons best known to itself Powershell wasn't sending the x-ms-version, so I fell back to telnet which did indeed produce the 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


来源:https://stackoverflow.com/questions/35719455/is-there-an-approach-to-force-a-azure-cdn-file-to-download-as-a-browser-attachme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!