Azure下载blob SAS - 请确认

时间:2014-05-23 09:13:07

标签: azure

我想确认一下我的代码,看看我是否在正确的道路上......

从我的视图中,我列出了属于该用户的所有blob。 用户可以单击其中一个blob并立即直接从blob存储本身开始下载。

以下是观点:

@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. 下载方法是公开的,这意味着它可以从网址调用。如果我想阻止这种情况,我可以在开始时使用linq方法并检查用户试图下载的blob是否在他/她的帐户中?那就够了吗?
  4. 我希望用户能够随时下载自己的blob。那么不需要设置starttime / endtime,对吧?我可以删除这些行吗?
  5. 更新

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

    发现冲突-----

      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>
    

1 个答案:

答案 0 :(得分:3)

  

我现在直接从blob存储下载吗?

没有。您在服务器上运行的代码正在下载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);
  

在using(MemoryStream ms = new MemoryStream())中,有什么用   这段代码呢?我使用的是不需要的东西吗?我可以做   用这些?女士,回应等等。

我认为代码很好。

  

下载方法是公开的,这意味着可以从网址调用它。   如果我想阻止这种情况,我可以在开始时使用linq方法   检查用户试图下载的blob是否属于他/她   帐户?那就够了吗?

在不知道更多细节的情况下,我认为不能回答这个问题。

  

我希望用户能够随时下载自己的blob。   那么不需要设置starttime / endtime,对吧?我可以删除   那些线?

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