Azure下载Blob SAS-请确认 [英] Azure download blob SAS - confirm please

查看:42
本文介绍了Azure下载Blob SAS-请确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确认我的代码,看看我在这里是否正确.

I would like to confirm my code and see if I'm on the right path here..

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

From my view i list all the blobs that belong to the user. The user can click on one of these blobs and start download it immediately and directly from blob storage itself.

这是视图:

@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. 在使用中(MemoryStream ms = new MemoryStream()),此代码做什么?我在使用不需要的东西吗?我应该使用这些吗?女士的位置,回应等等.
  3. 下载方法是公共的,这意味着可以从URL调用它.如果我想防止这种情况,我可以在开始时使用linq方法并检查用户尝试下载的Blob是否在他/她的帐户中吗?够了吗?
  4. 我希望用户能够随时下载自己的Blob.那么就不需要设置开始时间/结束时间了吧?我可以删除这些行吗?

更新

更新存储客户端库后,我开始出现以下错误.

After I updated the storage client library, I started to get the following error.

发现冲突-----

  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>

推荐答案

我现在是直接从Blob存储区中下载吗?

Am i downloading directly from the blob storage now?

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

No. Your code running on the server is downloading the blob and then server is streaming the code back to your client. To directly download from blob storage, replace the following lines of code:

// 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);

在使用中(MemoryStream ms = new MemoryStream()),做什么这个代码吗?我在使用不需要的东西吗?我可以做使用这些?女士的位置,回应等等.

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.

我认为代码很好.

下载方法是公共的,这意味着可以从URL调用它.如果我想防止这种情况,我可以在开始时使用linq方法吗?检查用户尝试下载的Blob是否在他/她的手中帐户?够了吗?

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?

在不知道更多细节的情况下,我认为这无法解决.

Without knowing more details, I don't think this can be answered.

我希望用户能够随时下载自己的Blob.那么就不需要设置开始时间/结束时间了吧?我可以删除吗那些线?

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的滥用.

You could. But it is not recommended. At the end of the day, SAS URL is a URL and your users may share with some other folks who are not supposed to download the images. By keeping the expiry date in the SAS URL, you're preventing misuse of the URL.

这篇关于Azure下载Blob SAS-请确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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