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

Cee*_*eeB 25 azure azure-storage azure-storage-blobs

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

ale*_*zyk 59

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

  string sasBlobToken = blob.GetSharedAccessSignature(sharedPolicy, new SharedAccessBlobHeaders()
            {
                ContentDisposition = "attachment; filename=" + friendlyFileName
            });
  string downloadLink = blob.Uri + sasBlobToken;
Run Code Online (Sandbox Code Playgroud)

  • 天才,正是我所寻找的! (5认同)
  • PLUSON E. 正是我所需要的 (2认同)

Njå*_*aug 39

允许用户在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);
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅此博文:http://blog.degree.no/2012/04/downloading-blobs-from-windows-azure/


sti*_*mms 16

截至2013年11月,现在支持Windows Azure Blob存储中的内容处置标头.您可以在创建blob或通过共享访问签名分配标头.

我修改了我的保存文档方法,以获取一个可选参数,这是要下载的友好名称.

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);
}
Run Code Online (Sandbox Code Playgroud)

更多关于它:http: //blog.simontimms.com/2013/12/02/content-disposition-comes-to-azure/