简体   繁体   English

如何避免C#Azure API的大型Blob上传内存不足?

[英]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.

相关问题 MVC大文件上传内存不足 - MVC Large File Uploads Running Out of Memory 下载 Blob 文件到 Memory Stream 从 Azure 使用 ZD7EFA19FBE74D3972FDZ5ADB6D - Download Blob file into Memory Stream from Azure using C# 如何在不使用磁盘且内存不足的情况下将大型文件从api流式传输到api? - How do I stream a large file from api to api without using disk and running out of memory? C#异步Ping:如何避免内存不足异常? - C# Async Ping: How to avoid an out of memory exception? 如何通过C#使用Trie避免内存不足 - How to avoid out of memory using trie by C# 使用C#XmlSerializer以大块方式写入大量对象,以避免内存不足 - Use C# XmlSerializer to write in chunks for large sets of objects to avoid Out of Memory 如何在不使用拆分(内存不足问题)C# 的情况下将大型 csv 文件转换为 json - How to convert from large csv file into json without using split (Out Of Memory issue) C# C#\\避免内存不足异常 - C# \ avoid out of memory exception 如何使用 c# 从 Azure 中删除 blob 不可变文件 - How to delete blob immutable file from Azure using c# 如何从c#中的URL将文件直接上传到azure blob? - How to upload a file directly to azure blob from the URL in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM