我正在从Blob中读取PDF文件,并使用Azure Function进行一些操作。当PDF大小为1.5GB(或更大)时,我的azure函数在代码命中以下命令后立即失败,并出现内存不足异常。
var ms = new MemoryStream();
log.LogInformation("Converting this File to memorystream : " + blob.Uri);
blob.DownloadToStream(ms); //Failes HERE.
我尝试增加计划,切换到EP3(14 GB内存+ 840 ACU)。但是问题还是一样。我还需要更改其他配置吗?如何护理它。
Microsoft.Azure.Storage.StorageException: Exception of type 'System.OutOfMemoryException' was thrown.
---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.IO.MemoryStream.set_Capacity(Int32 value)
at System.IO.MemoryStream.EnsureCapacity(Int32 value)
at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.MemoryStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Azure.Storage.Core.Util.AsyncStreamCopier`1.StartCopyStreamAsyncHelper(Nullable`1 copyLength, Nullable`1 maxLength, CancellationToken token)
at Microsoft.Azure.Storage.Core.Util.AsyncStreamCopier`1.StartCopyStreamAsync(Nullable`1 copyLength, Nullable`1 maxLength, CancellationToken cancellationToken)
at Microsoft.Azure.Storage.Core.Executor.Executor.ExecuteAsync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token)
--- End of inner exception stack trace ---
at Microsoft.Azure.Storage.Core.Executor.Executor.ExecuteAsync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token)
at Microsoft.Azure.Storage.Core.Executor.Executor.<>c__DisplayClass0_0`1.<ExecuteSync>b__0()
at Microsoft.Azure.Storage.Core.Util.CommonUtility.RunWithoutSynchronizationContext[T](Func`1 actionToRun)
at Microsoft.Azure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)
at Microsoft.Azure.Storage.Blob.CloudBlob.DownloadRangeToStream(Stream target, Nullable`1 offset, Nullable`1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
at Microsoft.Azure.Storage.Blob.CloudBlob.DownloadToStream(Stream target, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
This blog听起来似乎已经确定了问题-MemoryStream
使用int32来设置其容量,没有选择使用更大的数字。我无法完全将int32.MaxValue(2147483647)的值与指定大小(1.5GiB字节为1610612736字节)进行调和,但看起来足够接近罪魁祸首。该博客提出了一种向Blob存储中写入大量内容的解决方案,但我不认为这对于您的用例如何起作用。
如果您确实需要使用MemoryStream来满足您所使用的库的需求,那么将无法处理如此大的文件。