Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friendly filename when public download Azure blob

Is it possible to save a blob with a name of a GUID (or anything else) but when a user requests the files URI http://me.blob.core.windows.net/mycontainer/9BB34783-8F06-466D-AC20-37A03E504E3F the download comes down with a friendly name e.g. MyText.txt?

like image 333
CeejeeB Avatar asked Aug 11 '11 14:08

CeejeeB


People also ask

What is the other name of Blob storage present in Azure?

Page blobs store virtual hard drive (VHD) files and serve as disks for Azure virtual machines.

Why do we need azure file share when we have blobs?

Using Azure Blob Storage, the organization can store the tool and share the access link to its development team. In addition, some common scenarios in which users can utilize Azure Blob Storage are, Saving files for distributed access. Streaming audio or video data.


2 Answers

Now it is possible by setting the content-disposition header on generating the Shared Access Signature:

  string sasBlobToken = blob.GetSharedAccessSignature(sharedPolicy, new SharedAccessBlobHeaders()             {                 ContentDisposition = "attachment; filename=" + friendlyFileName             });   string downloadLink = blob.Uri + sasBlobToken; 
like image 173
alek kowalczyk Avatar answered Oct 02 '22 14:10

alek kowalczyk


Enabling users to download files (blobs) in Windows Azure can be done in 4 ways;

  • Direct Download – Set the Access of the Container to Public Read Access or Full Public Read Access and expose the URL to the end user. The drawback of this method is obviously security – you have no way of controlling access when the URL is exposed. There is also no way of detecting downloads, and to execute code before/after the download.

  • API Download – The user must run a Windows App, Silverlight etc. Also – the app must contain the storeaccount name and key – which might compromise security. Especially if you have several customers using the same account (they can still have their own containers of course).

  • Proxy Download – Have the user access a URL on YOUR server – which then retrieves the file from Azure and sends it to the user. The advantage of this is that you have full control of downloads, you can execute code before/after downloading and you don’t have to expose any Azure URL’s / account info. In fact – the end user will not even see that the file is stored in Azure. You can also override the filename this way. A downside is that all traffic passes through your server – so you' might get a bottleneck here.

  • Pass-through Download (Azure Shared Access Signature) - Creates a signature and inserts this signature in a URL where the user is redirected to Azure. The signature enables the user to access the file for a limited period of time. This is most likely your best option. It enables custom code to be executed before downloading, it will ensure max download speed for your users, and a good level of security is also ensured.

Here's a code snippet which streams files to user, and overrides the filename.

//Retrieve filenname from DB (based on fileid (Guid)) // *SNIP* string filename = "some file name.txt";   //IE needs URL encoded filename. Important when there are spaces and other non-ansi chars in filename. if (HttpContext.Current.Request.UserAgent != null &&     HttpContext.Current.Request.UserAgent.ToUpper().Contains("MSIE")) filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8).Replace("+", " ");  context.Response.Charset = "UTF-8"; //Important to set buffer to false. IIS will download entire blob before passing it on to user if this is not set to false context.Response.Buffer = false; context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); context.Response.AddHeader("Content-Length", "100122334"); //Set the length the file context.Response.ContentType = "application/octet-stream"; context.Response.Flush();  //Use the Azure API to stream the blob to the user instantly. // *SNIP* fileBlob.DownloadToStream(context.Response.OutputStream); 

See this blogpost for more: http://blog.degree.no/2012/04/downloading-blobs-from-windows-azure/

like image 24
Njål Arne Gjermundshaug Avatar answered Oct 02 '22 12:10

Njål Arne Gjermundshaug