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

查看:70
本文介绍了如何将文件从 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();

推荐答案

虽然 blob 内容可以通过 Web 服务器流式传输,并通过浏览器传输到最终用户,但此解决方案会增加 Web 服务器的负载,包括 CPU 和 NIC.

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.

另一种方法是向最终用户提供要下载的所需 blob 的 uri,他们可以在 html 内容中单击该 uri.例如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.

问题在于内容是否是私有的,因为除非使用公共 blob,否则上述 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;

请注意,开始时间设置为过去的几分钟.这是为了处理时钟漂移.这是完整教程 我从中获取/修改了此代码示例.

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 访问,您将完全绕过您的虚拟机/网络角色实例/网站实例(减少服务器负载),并让您的最终用户直接从 blob 存储中提取 blob 内容.您仍然可以使用您的网络应用来处理许可、决定要交付的内容等.但是……这可以让您直接链接到 Blob 资源,而不是通过您的网络服务器流式传输它们.

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天全站免登陆