將 Azure Blob 中的數據流式傳輸回客戶端 (Streaming Data from Azure Blobs Back to Client)


問題描述

將 Azure Blob 中的數據流式傳輸回客戶端 (Streaming Data from Azure Blobs Back to Client)

使用 ASP.Net Web API,我正在開發一項服務,該服務(除其他外)從 Azure 檢索數據,並將其返回給客戶端。

這樣做的一種方法是讀取netire blob 寫入緩衝區,然後將該緩衝區寫入響應。但是,我寧願流式傳輸內容,以獲得更好的性能。

Azure API 很簡單:

CloudBlobContainer container = BlobClient.GetContainerReference(containerName);
        CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
        using (var buffer = new MemoryStream())
        {
            await blob.DownloadToStreamAsync(buffer);
}

在代碼的其他地方,這會返回給客戶端:

HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(buffer);

但是我可以確定在客戶端完成讀取之前 MemoryStream 不會被關閉/處置嗎?


參考解法

方法 1:

As long as you don't wrap your memory stream in a "using" statement you will be fine. If you do use "using" you end up with a weird race condition where it works sometimes and fails at other times. I have code like yours in production and it works fine.

Only thing to be mindful of is that the whole blob is copied into memory before anything is sent to the client. This may cause memory pressures on your server and initial lag, depending on the size of the file. If that is a concern, you have a couple of options. One is to create a "lease" on the blob and give the user a URL to read it direct from blob storage for a limited time. That only works for low security scenarios though.

Alternatively you can use chunked transfer encoding. Basically, you read the file from blob storage in chunks and sends it to the client in those chunks. That saves memory ‑ but I have not been able to make it work async, so you are trading memory for threads. Which is the right solution for you will depend in your specific circumstances.

(I have not got the code to hand, post a comment if you want it and I'll try to dig it out, even if it's a bit old).

(by Saqibflytzen)

參考文件

  1. Streaming Data from Azure Blobs Back to Client (CC BY‑SA 2.5/3.0/4.0)

#azure #blob #stream #asp.net-web-api #C#






相關問題

將屬性添加到 Azure 表存儲中的實體似乎會將空屬性添加到表中的所有實體 (Adding property to entity in Azure Table Storage seems to add null property to all entities in table)

帶有 Azure WCF 服務的 Windows 8 應用 (Windows 8 App with Azure WCF Service)

當我嘗試查看連接到 TFS 的網站的部署選項卡時,無法檢索部署歷史記錄 (Failed to retrieve deployment history when I try to view the deployment tab of a web site connected to TFS)

是否可以在單個 Azure 項目中同時使用 New Relic 和 Azure Application Insights? (Is it possible to use both New Relic and Azure Application Insights together in single Azure project?)

有沒有辦法從 Azure 中獲取所有 DocumentDb 資源? (Is there a way to fetch all DocumentDb resoruces from Azure?)

Azure Search .Net SDK 中的複雜類型支持 (Complex Types support in Azure Search .Net SDK)

遠程服務器返回錯誤:(400) 錯誤請求。在 C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1 (The remote server returned an error: (400) Bad Request. At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1)

如何從狀態“False(MissingEndpoints)”啟用 kube-system/metrics-server? (How to enable kube-system/metrics-server from status "False (MissingEndpoints)"?)

通過 smtp 從安裝為 azure 中的 IaaS 的服務器發送電子郵件 (sending emails via smtp from a server installed as IaaS in azure)

cosmosDB RU 吞吐量如何強制執行 (How is cosmosDB RU throughput enforced)

在使用 azure 流量管理器和 azure 應用程序網關與 WAF 時實現國家級阻止 (Achieve country level blocking while using azure traffic manager and azure application gateway with WAF)

Azure 計算機視覺將圖像旋轉 180 度 (Azure Computer Vision Rotating the image By 180 degrees)







留言討論