[英]How to avoid C# Azure API from running out of memory for large blob uploads?
I'm trying to uploading very large (>100GB) blobs to Azure using Microsoft.Azure.Storage.Blob (9.4.2). 我正在尝试使用Microsoft.Azure.Storage.Blob(9.4.2)将非常大(> 100GB)的Blob上传到Azure。 However, it appears that even when using the stream-based blob write API, the library will allocate memory proportional to the size of the file (a 1.2GB test file results in a 2GB process memory footprint).
但是,看起来,即使使用基于流的Blob写入API,该库也会根据文件的大小分配内存(1.2GB的测试文件将导致2GB的进程内存占用)。 I need this to work in constant memory.
我需要它来保持不变的内存。 My code is below (similar results using UploadFromFile, UploadFromStream, etc.):
我的代码如下(使用UploadFromFile,UploadFromStream等类似的结果):
var container = new CloudBlobContainer(new Uri(sasToken));
var blob = container.GetBlockBlobReference("test");
const int bufferSize = 64 * 1024 * 1024; // 64MB
blob.StreamWriteSizeInBytes = bufferSize;
using (var writeStream = blob.OpenWrite())
{
using (var readStream = new FileStream(archiveFilePath, FileMode.Open))
{
var buffer = new byte[bufferSize];
var bytesRead = 0;
while ((bytesRead = readStream.Read(buffer, 0, bufferSize)) != 0)
{
writeStream.Write(buffer, 0, bytesRead);
}
}
}
This behavior is pretty baffling - I can see in TaskMgr that the upload indeed starts right away, so it's not like it's buffering things up waiting to send; 这种行为令人莫名其妙-我在TaskMgr中看到确实确实开始上传,所以它不像是在缓冲等待发送的内容。 there is no reason why it needs to hang on to previously sent data.
没有理由需要挂起以前发送的数据。 How does anyone use this API for non-trivial blob uploads?
有人如何使用此API进行非平凡的Blob上传?
I suggest you take a look at the BlobStorageMultipartStreamProvider sample, as it shows how a request stream can "forwarded" to an Azure Blob stream and this might reduce the amount of memory used at the server side while uploading. 我建议您看一下BlobStorageMultipartStreamProvider示例,因为它显示了请求流如何“转发”到Azure Blob流,并且这可能会减少上载时在服务器端使用的内存量。
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.