简体   繁体   English

Azure SAS下载Blob

[英]Azure SAS download blob

I'm trying to download a blob with SAS and kinda clueless right now. 我正在尝试下载带有SAS的Blob,并且现在有点无知。

I'm listing all the belonging user blobs in a view. 我在视图中列出了所有属于用户的Blob。 When a user clicks on the blob its supposed to start downloading it. 当用户单击Blob时,它应该开始下载它。

Here is the view: 这是视图:

@foreach (var file in Model)
{
        <a href='@Url.Action("GetSaSForBlob", "Folder", new { blob = file })>
        </a>
}

Here is my two functions located in "Folder" controller. 这是我在“文件夹”控制器中的两个功能。

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

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

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

static void DownloadFileTest(string blobSasUri)
{
    CloudBlockBlob blob = new CloudBlockBlob(new Uri(blobSasUri));
    using (MemoryStream ms = new MemoryStream())
    {
        blob.DownloadToStream(ms);
        byte[] data = new byte[ms.Length];
        ms.Position = 0;
        ms.Read(data, 0, data.Length);
    }
}
  1. What should i be passing from my view to GetSasForBlob? 我应该从我的观点传递给GetSasForBlob? At the moment CloudBlockBlob blob is null. 目前,CloudBlockBlob blob为null。

  2. Am i missing any code in function DownloadFileTest? 我是否在函数DownloadFileTest中缺少任何代码?

  3. Should i be calling DownloadFileTest directly from GetSasForBlob? 我应该直接从GetSasForBlob调用DownloadFileTest吗?

  4. How can i protect these two functions so people cant access them outside the view? 我如何保护这两个功能,使人们无法在视图外部访问它们? They are both static functions now. 它们现在都是静态函数。 I'm guessing that is not safe? 我猜这不安全吗?

1, What the value is of your file in your view. 1,在您的视图中file的值是多少。 I don't think MVC can create CloudBlockBlob object based on the file you provided. 我认为MVC不能根据您提供的文件创建CloudBlockBlob对象。 So this might be the reason you got null of your CloudBlockBlob . 因此,这可能是你得到的原因null你的CloudBlockBlob

2, In your DownloadFileTest you just download the binaries of the blob into the memory stream in your server and that's all. 2,在DownloadFileTest您只需将Blob的二进制文件下载到服务器中的内存流中即可。 If you need to let user download it to local disk you need to put the binaries into Response.Stream . 如果需要让用户将其下载到本地磁盘,则需要将二进制文件放入Response.Stream You can just use something like blob.DownloadToStram(Response.Stream) . 您可以只使用blob.DownloadToStram(Response.Stream)类的东西。

3, That's up to you. 3,这取决于您。 You can merge them in the same method if you want. 您可以根据需要以相同的方法合并它们。

4, If you want user to download blob through your web front (website or web service), as what you are doing now, you need to set your blob container as private and secure your website or web service by using something loke [Authorize] attribute. 4,如果您希望用户通过您的Web前端(网站或Web服务)下载Blob(如您现在所做的那样),则需要将Blob容器设置为private容器,并通过使用类似的方法来保护您的网站或Web服务的安全[Authorize]属性。 Basically in your case you really don't need to use SAS at all because all download requests are performed through your web front. 从根本上来说,您实际上根本不需要使用SAS,因为所有下载请求都是通过Web前端执行的。

Hope this helps a bit. 希望这个对你有帮助。

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

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