Azure的下载大型文件的GetSharedAccessSignature [英] Azure download with GetSharedAccessSignature of large file

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

问题描述

通过一个网页,我想允许用户下载存储在Azure云存储(超大型)文件。我用的是GetSharedAccessSignature允许用户下载它(注:集装箱被保护,而不是公共可读)。
我用下面的code:

Through a webpage, I want to allow users to download (very large) file stored on Azure Cloud storage. I use the GetSharedAccessSignature to allow users to download it (note: the Container is protected and not publicly readable). I use the following code:

// get the file blob
// CloudBlockBlob blob set previously and check (it exists)

        string sas = blob.GetSharedAccessSignature(
            new SharedAccessPolicy() { 
                Permissions = SharedAccessPermissions.Read,
                SharedAccessStartTime = DateTime.Now.AddMinutes(-5),
                SharedAccessExpiryTime = DateTime.Now.AddMinutes(54) 
            });

        Response.Redirect(blob.Attributes.Uri.AbsoluteUri + sas);

这工作时,下载需要1个多小时除(就像我说的,非常大的文件被下载超过不太好互联网连接...)。
如果下载时间较长,发生超时并且下载停止的共享访问到期的时间已经过去了。

This works EXCEPT when the download takes more than 1 hour (as I said, very large files being downloaded over not so good internet connection...). If the download takes longer, a time-out occurs and the download stops as the Shared Access Expired Time has passed.

我该如何解决呢?

亲切的问候,
罗比·德·苏特尔

Kind regards, Robbie De Sutter

推荐答案

从code,看来你实际上是让浏览器下载。在这种情况下,我认为选择是有共同的有效访问签名持续时间较长。

From the code, it seems that you're actually letting the browser do the download. In that scenario, I believe the option is to have shared access signatures valid for longer duration.

然而,另一个想法是让你的code读取blob的块,然后传送这些块。如果有需要,你可以创建一个新的SAS URI和使用,如果SAS URI超时已过期,而该blob还没有完全下载的方式。

Yet another idea is to have your code read the chunks of the blob and then transmit those chunks. That way if there's a need you could create a new SAS URI and use that if the SAS URI timeout has expired and the blob has not downloaded completely.

看看下面的示例code。基本上这个示例中,我有一个200 MB的blob。在循环中,我的code读取BLOB的1 MB块并将该块了。

Take a look at sample code below. Basically in this sample, I have a 200 MB blob. In loop, my code reads 1 MB chunk of the blob and sends that chunk down.

        protected void ButtonDownload_Click(object sender, EventArgs e)
    {
        var blobUri = "http://127.0.0.1:10000/devstoreaccount1/abc/test2.vhd?sr=b&st=2012-08-17T10%3A29%3A46Z&se=2012-08-17T11%3A29%3A46Z&sp=r&sig=dn7cnFhP1xAPIq0gH6klc4nifqgk7jfOgb0hV5Koi4g%3D";
        CloudBlockBlob blockBlob = new CloudBlockBlob(blobUri);
        var blobSize = 200 * 1024 * 1024;
        int blockSize = 1024 * 1024;
        Response.Clear();
        Response.ContentType = "APPLICATION/OCTET-STREAM";
        System.String disHeader = "Attachment; Filename=\"" + blockBlob.Name + "\"";
        Response.AppendHeader("Content-Disposition", disHeader);
        for (long offset = 0; offset < blobSize; offset += blockSize)
        {
            using (var blobStream = blockBlob.OpenRead())
            {
                if ((offset + blockSize) > blobSize)
                    blockSize = (int)(blobSize - offset);
                byte[] buffer = new byte[blockSize];
                blobStream.Read(buffer, 0, buffer.Length);
                Response.BinaryWrite(buffer);
                Response.Flush();
            }
        }
        Response.End();
    }

希望这有助于。

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

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