首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >公开下载Azure blob时的友好文件名

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

Stack Overflow用户
提问于 2011-08-11 22:28:40
回答 6查看 14.7K关注 0票数 30

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

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-04-25 18:16:16

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

  • Direct下载-将容器的访问权限设置为公共读取访问权限或完全公共读取访问权限,并将其公开给最终用户。这种方法的缺点显然是安全性-当URL公开时,您无法控制访问。也没有办法检测下载,并在download.
  • API下载之前/之后执行代码-用户必须运行Windows应用程序,Silverlight等。此外,应用程序必须包含商店帐户名和密钥-这可能会危及安全性。特别是如果你有几个客户使用相同的帐户(他们仍然可以有自己的course).
  • Proxy Download容器-让用户访问你的服务器上的URL -然后从Azure检索文件并将其发送给用户。这样做的好处是你可以完全控制下载,你可以在下载之前/之后执行代码,并且你不必公开任何Azure URL/帐户信息。事实上,最终用户甚至不会看到文件存储在Azure中。你也可以用的方式覆盖文件名。缺点是所有的流量都要经过你的服务器--所以你可能会遇到瓶颈。Azure Download (Azure共享访问签名)-创建一个签名,并将此签名插入到用户被重定向到Azure的URL中。该签名使用户能够在有限的时间内访问该文件。这很可能是你最好的选择。支持自定义代码在下载前执行,为用户提供最快的下载速度,并保证良好的安全性。

下面是一段代码片段,它将文件流式传输给用户,并覆盖文件名。

代码语言:javascript
复制
//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/

票数 43
EN

Stack Overflow用户

发布于 2015-06-10 21:28:31

现在可以通过在生成共享访问签名时设置content-disposition头部:

代码语言:javascript
复制
  string sasBlobToken = blob.GetSharedAccessSignature(sharedPolicy, new SharedAccessBlobHeaders()
            {
                ContentDisposition = "attachment; filename=" + friendlyFileName
            });
  string downloadLink = blob.Uri + sasBlobToken;
票数 81
EN

Stack Overflow用户

发布于 2013-12-03 05:26:04

从2013年11月起,Windows Azure Blob存储中现在支持content-disposition标头。您可以在创建blob时分配标头,也可以通过共享访问签名分配标头。

我修改了我的save document方法,以获取一个可选参数,该参数是要下载的友好名称。

代码语言:javascript
复制
public void SaveDocument(Stream documentToSave, string contentType, string containerName, string url, string friendlyName = null)
{
    var container = GetContainer(containerName);
    container.CreateIfNotExists();
    var blob = container.GetBlockBlobReference(url);
    blob.Properties.ContentType = contentType;
    if (friendlyName != null)
      blob.Properties.ContentDisposition = "attachment; filename=" + friendlyName;
    blob.UploadFromStream(documentToSave);
}

更多信息:http://blog.simontimms.com/2013/12/02/content-disposition-comes-to-azure/

票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7027656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档