Azure的下载BLOB FILESTREAM / MemoryStream的 [英] Azure download blob filestream/memorystream

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

问题描述

我希望用户能够从我的网站下载的斑点。我想这样做最快/ cheapeast /最好的方法。

I want users to be able to download blobs from my website. I want the fastest/cheapeast/best way to do this.

继承人什么我想出了:

        CloudBlobContainer blobContainer = CloudStorageServices.GetCloudBlobsContainer();

        CloudBlockBlob blob = blobContainer.GetBlockBlobReference(blobName);
        MemoryStream memStream = new MemoryStream();
        blob.DownloadToStream(memStream);

        Response.ContentType = blob.Properties.ContentType;
        Response.AddHeader("Content-Disposition", "Attachment; filename=" + fileName + fileExtension);
        Response.AddHeader("Content-Length", (blob.Properties.Length).ToString());
        Response.BinaryWrite(memStream.ToArray());
        Response.End();

我使用的MemoryStream,但现在即时通讯猜我应该FILESTREAM去,因为掉斑点是,在某些情况下,大...对吧?

I'm using memorystream now but im guessing i should go with filestream because off the blobs being, in some cases, large.. Right?

我FILESTREAM尝试过,但我失败惨..想你可以给我一些code为FILESTREAM?

I tried it with filestream but I failed miserable.. Think you could give me some code for filestream?

推荐答案

恕我直言,最便宜,最快速的解决办法是直接从Blob存储下载。目前您的code先下载服务器上的斑点,并从那里流。你可以做的反而是创建一个共享访问签名许可和内容处置头设置和创建基于BLOB网址,并使用该URL。在这种情况下,斑点的内容将被直接从存储到客户端浏览器流

IMHO, the cheapest and fastest solution would be directly downloading from blob storage. Currently your code is first downloading the blob on your server and streaming from there. What you could do instead is create a Shared Access Signature with Read permission and Content-Disposition header set and create blob URL based on that and use that URL. In this case, the blob contents will be directly streamed from storage to the client browser.

例如看看下面的code:

For example look at the code below:

    public ActionResult Download()
    {
        CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("accountname", "accountkey"), true);
        var blobClient = account.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference("container-name");
        var blob = container.GetBlockBlobReference("file-name");
        var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            {
                Permissions = SharedAccessBlobPermissions.Read,
                SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
            }, new SharedAccessBlobHeaders()
            {
                ContentDisposition = "attachment; filename=file-name"
            });
        var blobUrl = string.Format("{0}{1}", blob.Uri, sasToken);
        return Redirect(blobUrl);
    }

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

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