公开下载 Azure blob 时的友好文件名 [英] Friendly filename when public download Azure blob

查看:17
本文介绍了公开下载 Azure blob 时的友好文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 GUID 名称(或其他任何名称)保存 blob,但是当用户请求文件 URI http://me.blob.core.windows.net/mycontainer/9BB34783-8F06-466D-AC20-37A03E504E3F 一个友好的名称下载下来例如MyText.txt?

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?

推荐答案

允许用户在 Windows Azure 中下载文件(blob)可以通过 4 种方式完成;

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

  • 直接下载 – 将容器的访问权限设置为公共读取访问权限或完全公共读取访问权限,并将 URL 公开给最终用户.这种方法的缺点显然是安全性——当 URL 暴露时,你无法控制访问.也无法检测下载,也无法在下载之前/之后执行代码.

  • 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 下载 – 用户必须运行 Windows 应用程序、Silverlight 等.此外 – 应用程序必须包含 storeaccount 名称和密钥 – 这可能会危及安全.特别是如果您有多个客户使用同一个帐户(当然他们仍然可以拥有自己的容器).

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).

代理下载 – 让用户访问您服务器上的 URL – 然后从 Azure 检索文件并将其发送给用户.这样做的好处是您可以完全控制下载,您可以在下载之前/之后执行代码,并且您不必公开任何 Azure URL 的/帐户信息.事实上,最终用户甚至不会看到文件存储在 Azure 中.您也可以通过这种方式覆盖文件名.缺点是所有流量都通过您的服务器——因此您可能会在这里遇到瓶颈.

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.

传递下载(Azure 共享访问签名) - 创建签名并将此签名插入到用户重定向到 Azure 的 URL 中.签名使用户能够在有限的时间内访问文件.这很可能是您的最佳选择.它允许在下载之前执行自定义代码,它将确保您的用户的最大下载速度,并且还确保了良好的安全性.

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);

查看此博文了解更多信息:http://blog.degree.no/2012/04/downloading-blob-from-windows-azure/

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

这篇关于公开下载 Azure blob 时的友好文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆