Azure的SAS下载的blob [英] Azure SAS download blob

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

问题描述

我想下载使用SAS一个bl​​ob还挺无能现在。

我列出了所有在视图中属于用户的斑点。当用户点击BLOB它应该开始下载。

下面认为:

  @foreach(以型号var文件)
{
        < A HREF =@ Url.Action(GetSaSForBlob,文件夹,新的{BLOB =文件})>
        &所述; / A>
}

下面是位于文件夹控制我的两个功能。

 公共无效GetSaSForBlob(CloudBlockBlob BLOB)
{
    VAR SAS = blob.GetSharedAccessSignature(新SharedAccessBlobPolicy()
    {
        SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5),
        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(3),
        权限= SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write,
    });    DownloadFileTest(的String.Format(CultureInfo.InvariantCulture,{0} {1},blob.Uri,SAS));    //返回的String.Format(CultureInfo.InvariantCulture,{0} {1},blob.Uri,SAS);
}静态无效DownloadFileTest(字符串blobSasUri)
{
    CloudBlockBlob BLOB =新CloudBlockBlob(新的URI(blobSasUri));
    使用(MemoryStream的毫秒=新的MemoryStream())
    {
        blob.DownloadToStream(毫秒);
        字节[]数据=新的字节[ms.Length]
        ms.Position = 0;
        ms.Read(数据,0,data.Length);
    }
}


  1. 我应该怎么从我的观点传递给GetSasForBlob?目前CloudBlockBlob BLOB为null。


  2. 我失去了任何code函数DownloadFileTest?


  3. 我应该直接从GetSasForBlob调用DownloadFileTest?


  4. 我如何保护这两个功能让人们不能认为外部访问它们?他们都是静态的功能了。我猜,是不是安全?



解决方案

1,该值是你的文件在你看来什么。我不认为MVC可以根据您提供的文件中创建 CloudBlockBlob 对象。所以这可能是你得到的原因你的 CloudBlockBlob

2,在你的 DownloadFileTest 您只需下载blob的二进制文件到你的服务器内存流,这一切。如果您需要让用户将其下载到本地磁盘,你需要把二进制文件到 Response.Stream 。你可以使用类似 blob.DownloadToStram(Response.Stream)

3,这是给你的。如果你想你可以用同样的方法将它们合并。

4,如果你想用户通过您的web前端(网站或网络服务)下载一滴,因为你现在在做什么,你需要设置你的blob容器作为私人并通过使用一些魔神 [授权] 属性,保护您的网站或网络服务。基本上你的情况,你真的不需要在所有的,因为所有的下载请求都通过您的面前表演使用SAS。

希望这有助于一点。

I'm trying to download a blob with SAS and kinda clueless right now.

I'm listing all the belonging user blobs in a view. When a user clicks on the blob its supposed to start downloading it.

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? At the moment CloudBlockBlob blob is null.

  2. Am i missing any code in function DownloadFileTest?

  3. Should i be calling DownloadFileTest directly from GetSasForBlob?

  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. I don't think MVC can create CloudBlockBlob object based on the file you provided. So this might be the reason you got null of your CloudBlockBlob.

2, In your DownloadFileTest you just download the binaries of the blob into the memory stream in your server and that's all. If you need to let user download it to local disk you need to put the binaries into Response.Stream. You can just use something like blob.DownloadToStram(Response.Stream).

3, That's up to you. 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. Basically in your case you really don't need to use SAS at all because all download requests are performed through your web front.

Hope this helps a bit.

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

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