如何将文件从Azure的Blob存储下载到浏览器 [英] How to download a file to browser from Azure Blob Storage

查看:205
本文介绍了如何将文件从Azure的Blob存储下载到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地列出可用的文件,但我需要知道我怎么能向下传递文件到浏览器用户下载,没有它不一定保存到服务器

I'm already successfully listing available files, but I needed to know how I could pass that file down to the browser for a user to download without necessarily saving it to the server

下面是如何得到的文件列表

Here is how I get the list of files

var azureConnectionString = CloudConfigurationManager.GetSetting("AzureBackupStorageConnectString");
var containerName = ConfigurationManager.AppSettings["FmAzureBackupStorageContainer"];
if (azureConnectionString == null || containerName == null)
    return null;

CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(azureConnectionString);
var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
var container = backupBlobClient.GetContainerReference(containerName); 
var blobs = container.ListBlobs(useFlatBlobListing: true);
var downloads = blobs.Select(blob => blob.Uri.Segments.Last()).ToList();


推荐答案

虽然斑点内容可以通过Web服务器进行流,并且沿着通过浏览器最终用户,这种解决方案使负载在web服务器上,包括C​​PU和网卡

While blob content may be streamed through a web server, and along to the end user via browser, this solution puts load on the web server, both cpu and NIC.

的另一种方法是提供一种具有一个URI到所需的斑点被下载,其可能在html内容点击终端用户。例如 https://myaccount.blob.core.windows.net/mycontainer/myblob.ext

An alternative approach is to provide the end user with a uri to the desired blob to be downloaded, which they may click in the html content. e.g. https://myaccount.blob.core.windows.net/mycontainer/myblob.ext.

本的问题是,如果内容是私人的,因为一个URI如所述一个以上不会除非使用公斑点工作。对于这一点,你可以创建一个共享访问签名(或服务器存储策略),然后导致追加到URI的散列查询字符串。这个新的URI将是有效的给定时间长度(10分钟,例如)。

The issue with this is if the content is private, since a uri such as the one above won't work unless using public blobs. For this, you can create a Shared Access Signature (or server-stored Policy), which then results in a hashed querystring appended to the uri. This new uri would be valid for a given length of time (10 minutes, for example).

下面是一个blob创建SAS的一个小例子:

Here's a small example of creating an SAS for a blob:

var sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

var sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);

return blob.Uri + sasBlobToken;

请注意,该开始时间设定为在过去几分钟。这是处理时钟漂移。这里是<一个href=\"http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-shared-access-signature-part-2/\">full教程我抓住/从修改此code样本。

Note that the start time is set to be a few minutes in the past. This is to deal with clock-drift. Here is the full tutorial I grabbed/modified this code sample from.

通过使用直接访问的blob,你会完全绕过你的VM / Web角色实例/网站实例(减少了服务器负载),并直接从Blob存储有你的最终用户拉BLOB内容。你仍然可以使用你的Web应用程序来处理的权限管理,决定提供哪些内容等,但...这可以让你直接链接一滴资源,而不是通过你的Web服务器流式传输它们。

By using direct blob access, you will completely bypass your VM/web role instance/web site instance (reducing server load), and have your end-user pull blob content directly from blob storage. You can still use your web app to deal with permissioning, deciding which content to deliver, etc. But... this lets you direct-link to blob resources, rather than streaming them through your web server.

这篇关于如何将文件从Azure的Blob存储下载到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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