gpt4 book ai didi

Azure 下载 blob SAS - 请确认

转载 作者:行者123 更新时间:2023-12-03 01:10:52 25 4
gpt4 key购买 nike

我想确认我的代码并看看我是否走在正确的道路上..

从我的角度来看,我列出了属于该用户的所有 blob。用户可以单击这些 Blob 之一,然后立即开始直接从 Blob 存储本身下载它。

这是 View :

@model IEnumerable<Delamapp.CloudStorageServices.UploadEntity>

@foreach (var file in Model)
{
<a href='@Url.Action("DownloadFileTest", "Folder", new { blobName = file.BlobName, fileName = file.FileName, fileExtension = file.FileExtension })'

@file.FileName.PreviewString(file.FileName, file.FileExtension)@file.FileExtension
}

这是下载功能:

   public void DownloadFileTest(string blobName, string fileName, string fileExtension)
{
//Get SAS url
CloudBlobContainer blobContainer = CloudStorageServices.GetCloudBlobsContainer();
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(blobName);

var sas = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5),
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(3),
Permissions = SharedAccessBlobPermissions.Read
});

string blobSasUri = (string.Format(CultureInfo.InvariantCulture, "{0}{1}", blob.Uri, sas));

//Download Blob through SAS url
CloudBlockBlob blobSas = new CloudBlockBlob(new Uri(blobSasUri));
using (MemoryStream ms = new MemoryStream())
{
blobSas.DownloadToStream(ms);
byte[] data = new byte[ms.Length];
ms.Position = 0;
ms.Read(data, 0, data.Length);

Response.ContentType = blobSas.Properties.ContentType;
Response.AddHeader("Content-Disposition", "Attachment; filename=" + fileName + fileExtension);
Response.AddHeader("Content-Length", (blobSas.Properties.Length).ToString());
Response.BinaryWrite(ms.ToArray());
Response.End();
}
}
  1. 我现在直接从 blob 存储下载吗?
  2. 在using(MemoryStream ms = new MemoryStream())中,这段代码做了什么?我是否使用了不需要的东西?我应该使用这些吗?女士的立场、回应..等等
  3. Download 方法是公共(public)的,这意味着可以从 url 调用它。如果我想防止这种情况,我可以在开始时使用 linq 方法并检查用户尝试下载的 blob 是否在他/她的帐户中吗?这样就够了吗?
  4. 我希望用户能够随时下载自己的 blob。那么就不需要设置开始时间/结束时间了,对吗?我可以删除这些行吗?

更新

更新存储客户端库后,我开始收到以下错误。

发现冲突-----

  1>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(1635,5): warning MSB3247: Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file: <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Microsoft.Data.Edm" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.6.1.0" newVersion="5.6.1.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Microsoft.Data.Services.Client" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.6.1.0" newVersion="5.6.1.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Spatial" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.6.1.0" newVersion="5.6.1.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Microsoft.Data.OData" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.6.1.0" newVersion="5.6.1.0" /></dependentAssembly></assemblyBinding>

最佳答案

Am i downloading directly from the blob storage now?

没有。您在服务器上运行的代码正在下载 blob,然后服务器将代码流式传输回您的客户端。要直接从 blob 存储下载,请替换以下代码行:

// Download Blob through SAS url
CloudBlockBlob blobSas = new CloudBlockBlob(new Uri(blobSasUri));
using (MemoryStream ms = new MemoryStream())
{
blobSas.DownloadToStream(ms);
byte[] data = new byte[ms.Length];
ms.Position = 0;
ms.Read(data, 0, data.Length);
Response.ContentType = blobSas.Properties.ContentType;
Response.AddHeader("Content-Disposition", "Attachment; filename=" + fileName + fileExtension);
Response.AddHeader("Content-Length", (blobSas.Properties.Length).ToString());
Response.BinaryWrite(ms.ToArray());
Response.End();
}

// Download Blob through SAS url
Redirect(blobSasUri);

Within the using (MemoryStream ms = new MemoryStream()), what does this code do? Am i using something that is not needed? Should i be using these? Ms.position, response.. etc.

我认为代码很好。

Download method is public which means it can be called from the url. If i want to prevent this, can i use a linq method in the start and check if the blob the user is trying to download is in his/hers account? Would that be enough?

在不了解更多细节的情况下,我认为无法回答这个问题。

I want users to be able to download their own blobs at anytime. Setting starttime/endtime is not need then, right? Can i just delete those lines?

你可以。但不建议这样做。归根结底,SAS URL 是一个 URL,您的用户可能会与其他一些不应该下载图像的人共享。通过在 SAS URL 中保留到期日期,您可以防止滥用该 URL。

关于Azure 下载 blob SAS - 请确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23825246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com