公开下载Azure blob时的友好文件名

可以使用GUID的名称(或其他任何东西)来保存一个Blob,但是当用户请求文件URI http://me.blob.core.windows.net/mycontainer/9BB34783-8F06-466D-AC20-37A03E504E3F时,下载文件会以友好的名字命名. MyText.txt?
使用户能够在Windows Azure中下载文件(blob)可以通过4种方式完成;

>直接下载 – 将容器的访问权限设置为公共读取访问权限或完全公共访问权限,并将URL公开给最终用户.这种方法的缺点显然是安全的 – 当URL暴露时,你无法控制访问.还没有办法检测下载,并且在下载之前/之后执行代码.
> API下载 – 用户必须运行Windows应用程序,Silverlight等.此外 – 应用程序必须包含storeaccount名称和密钥 – 这可能会危及安全性.特别是如果你有几个客户使用相同的帐号(他们当然可以拥有自己的容器).
>代理下载 – 让用户访问您的服务器上的URL – 然后从Azure中检索文件并将其发送给用户.这样做的好处是,您可以完全控制下载,您可以在下载之前/之后执行代码,并且不必公开任何Azure URL /帐户信息.实际上,最终用户甚至不会看到该文件存储在Azure中.您也可以通过这种方式覆盖文件名.所有的流量都会通过您的服务器,这样就可以在这里遇到瓶颈.
>传递下载(Azure共享访问签名) – 创建签名,并将该签名插入用户重定向到Azure的URL中.签名使用户能够在有限的时间内访问该文件.这最有可能是你最好的选择.它允许在下载之前执行自定义代码,它将确保您的用户的最大下载速度,并且还确保良好的安全级别.

这是一个代码片段,将文件流式传输给用户,并覆盖文件名.

//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-blobs-from-windows-azure/

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...