30

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

4

6 回答 6

85

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

  string sasBlobToken = blob.GetSharedAccessSignature(sharedPolicy, new SharedAccessBlobHeaders()
            {
                ContentDisposition = "attachment; filename=" + friendlyFileName
            });
  string downloadLink = blob.Uri + sasBlobToken;
于 2015-06-10T13:28:30.647 回答
45

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

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

  • API 下载- 用户必须运行 Windows 应用程序、Silverlight 等。此外 - 应用程序必须包含商店帐户名称和密钥 - 这可能会危及安全性。特别是如果您有多个客户使用同一个帐户(当然,他们仍然可以拥有自己的容器)。

  • 代理下载– 让用户访问您服务器上的 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/

于 2012-04-25T10:16:15.643 回答
21

截至 2013 年 11 月,现在支持 Windows Azure Blob 存储中的 content-disposition 标头。您可以在创建 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);
}

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

于 2013-12-02T21:26:04.197 回答
4

答案是不。您需要在 blob 上设置 content-disposition 标头,并且无法在 blob 存储中设置该标头。

于 2011-08-11T17:45:03.587 回答
0

首先,您可以将自己的自定义域用于 blob URI:http: //blogs.msdn.com/b/avkashchauhan/archive/2011/03/22/using-custom-domain-name-with-windows-azure-storage-代替 Windows-stroage-name-blob-core-windows-net.aspx

为您提供解决方案的示例...

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(ConfigurationManager.AppSettings["WindowsAzureStorageAccountName"], ConfigurationManager.AppSettings["WindowsAzureStorageAccountKey"]);
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(credentials, true);

CloudBlobContainer blobContainer = blobClient.GetContainerReference("mycontainer");

// e.g. GUID of currently logged in user
string fileName = System.Web.Security.Membership.GetUser().ProviderUserKey.ToString();

CloudBlob blob = blobContainer.GetBlobReference(fileName);

byte[] blobArray = blob.DownloadByteArray();

Response.ContentType = "text/plain";
Response.AddHeader("content-disposition", "attachment; filename=" + "MyText.txt");
Response.BinaryWrite(blobArray);
Response.End();
于 2011-08-11T15:04:30.437 回答
0

我尝试通过代码和手动设置 ContentDisposition 属性。它对我不起作用。我调整了一些东西,它开始工作了。这可能很奇怪,但是content-dispositionresposne 中的标头丢失了,直到我Properties and Metadata在 blob 上传期间向两者添加了文件名。

NuGet 包:WindowsAzure.Storage(版本=9.3.3)
存储帐户类型:StorageV2(通用 v2)

        var cloudStorageAccount = CloudStorageAccount.Parse(chatStorageConnectionString);
        var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
        string extension = ".jpg";
        string containerName = "foobar";

        var container = cloudBlobClient.GetContainerReference(containerName);

        if(!(await container.ExistsAsync()))
        {
            logger.LogError($"Unable to connect to container {containerName: containerName}");
            return "";
        }

        var imageBlob = container.GetBlockBlobReference(Guid.NewGuid().ToString() + extension);

        // These two lines are what I'm talking about
        imageBlob.Metadata.Add("ContentDisposition", "attachment; filename=\"testfire.jpg\"");
        imageBlob.Properties.ContentDisposition = "attachment; filename=\"testfire.jpg\"";

        await imageBlob.UploadFromStreamAsync(stream);
        if(!await imageBlob.ExistsAsync())
        {
            logger.LogError("Image was not uploaded succesfully.");
            return "";
        }
于 2020-11-15T13:41:22.957 回答