简体   繁体   English

使用 AspNet 从 Azure Blob 存储下载和重命名文件

[英]Download and Rename Files from Azure Blob Storage with AspNet

I am developing a web platform to manage the upload/download of files.我正在开发一个 web 平台来管理文件的上传/下载。 The front-end is developed in React, the back-end in ASP.NET and Azure Blob Containers is used to store the uploaded files.前端使用 React 开发,后端使用 ASP.NET 和 Azure Blob Containers 来存储上传的文件。

As for the upload, I'm using the Microsoft "Azure Storage Client Library" to send files directly from the client to Azure through SAS authentication.至于上传,我使用微软的“Azure 存储客户端库”通过 SAS 身份验证,将文件直接从客户端发送到 Azure。 This Javascript library allow me to update a progress bar during the whole process.这个 Javascript 库允许我在整个过程中更新进度条。

As for the download, the process is more complicated: the file is first downloaded from the server (phase 1 or Azure->Server) and then it is downloaded from the client (phase 2 or Server->Client).至于下载,过程比较复杂:首先从服务器下载文件(阶段1或Azure->Server),然后从客户端下载(阶段2或Server->Client)。 Phase 1 creates two problems for me:第一阶段给我带来了两个问题:

  • I cannot display a progress bar to check the progress;我无法显示进度条来检查进度;
  • It can take long time and, at this stage, the client cannot begin the download;这可能需要很长时间,在此阶段,客户端无法开始下载;

To solve these problems I would like one of the following solutions:为了解决这些问题,我想要以下解决方案之一:

  • download the file directly from the client using the Javascript library but in this case it is necessary to rename the file ;使用 Javascript 库直接从客户端下载文件,但在这种情况下需要重命名文件
  • create a server-client communication to implement a progress bar relating to phase 1;创建服务器-客户端通信以实现与阶段 1 相关的进度条;

This is my current C # function to allow the download of a file这是我当前的 C #function 允许下载文件

using Microsoft.WindowsAzure.Storage.Blob;

private IActionResult DownloadFile(...) {
    ...
    using (var blobStream = new MemoryStream()) {
        string blobName = ...
        CloudBlobContainer cloudBlobContainer = ...
        CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(blobName);
        blob.DownloadToStream(blobStream);
        return new FileContentResult(blobStream.ToArray(), "application/pdf");
    }
}

EDIT:编辑:

Below the code I use to generate the SAS token:在我用来生成 SAS 令牌的代码下方:

private string GetSasReadToken(string connectionString, string containerName) {

    var storageAccount = CloudStorageAccount.Parse(connectionString);
    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
    var sasConstraints = new SharedAccessBlobPolicy {
        SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(60),
        Permissions = SharedAccessBlobPermissions.Read
    };
    var sasContainerToken = cloudBlobContainer.GetSharedAccessSignature(sharedAccessBlobPolicy);
}

In order to make use of Content-Disposition , you will need to generate SAS token on a blob (currently you're creating a SAS token on a blob container).为了使用Content-Disposition ,您需要在 blob 上生成 SAS 令牌(当前您正在 blob 容器上创建 SAS 令牌)。 Then you will need to make use of SharedAccessBlobHeaders and define the content-disposition value there.然后,您将需要使用SharedAccessBlobHeaders并在那里定义 content-disposition 值。

Here's the sample code (untested though):这是示例代码(虽然未经测试):

private string GetSasReadToken(string connectionString, string containerName, string blobName) {

    var storageAccount = CloudStorageAccount.Parse(connectionString);
    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(blobName);
    var sasConstraints = new SharedAccessBlobPolicy {
        SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(60),
        Permissions = SharedAccessBlobPermissions.Read,
    };
    var sasHeaders = new SharedAccessBlobHeaders();
    sasHeaders.ContentDisposition = "attachment;filename=<your-download-file-name>";
    var sasBlobToken = cloudBlockBlob.GetSharedAccessSignature(sharedAccessBlobPolicy, sasHeaders);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM