[英]Getting "The inner stream position has changed unexpectedly" in AWS Lambda
I am implementing a file upload in ASP.Net core.我正在 ASP.Net 核心中实现文件上传。 Everything works fine when testing locally on Windows but when I deploy my code on AWS Lambda, I am getting在 Windows 上本地测试时一切正常,但是当我在 AWS Lambda 上部署我的代码时,我得到
"System.InvalidOperationException: The inner stream position has changed unexpectedly. at Microsoft.AspNetCore.Http.Internal.ReferenceReadStream.VerifyPosition() at Microsoft.AspNetCore.Http.Internal.ReferenceReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.IO.Stream.CopyTo(Stream destination, Int32 bufferSize)" “System.InvalidOperationException:内部流位置意外更改。在 Microsoft.AspNetCore.Http.Internal.ReferenceReadStream.VerifyPosition() 在 Microsoft.AspNetCore.Http.Internal.ReferenceReadStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 计数) 在 System.IO.Stream.CopyTo(Stream destination, Int32 bufferSize)"
My code:我的代码:
[HttpPost]
[Route("")]
[Authorize]
public IActionResult Store([FromForm] MyFiles files)
{
var stream1 = files.File1.OpenReadStream();
var stream2 = files.File2.OpenReadStream();
string result;
using (MemoryStream ms = new MemoryStream())
{
stream1.CopyTo(ms);
ms.Position = 0;
result= GetCrcForFile(ms);
}
}
public class MyFiles
{
public IFormFile File1 { get; set; }
public IFormFile File2 { get; set; }
}
public string GetCrcForFile(Stream result)
{
uint crc = 0;
using (MemoryStream ms = new MemoryStream())
{
result.CopyTo(ms);
var bytes = ms.ToArray();
crc = Crc32Algorithm.Compute(bytes);
return crc.ToString("X");
}
}
The exception happens on line result.CopyTo(ms);
异常发生在线result.CopyTo(ms);
I am not sure if it is .Net Core behaving differently on Linux environment or AWS Lambda issue or I am doing something wrong.我不确定是 .Net Core 在 Linux 环境或 AWS Lambda 问题上的行为不同,还是我做错了什么。
As indicated in this issue , depending on what kind of server you're using, you cannot access the file streams in just any order.如本期所述,根据您使用的服务器类型,您不能以任何顺序访问文件流。 You need to open and process the files in sequential order, or you'll receive this "The inner stream position has changed unexpectedly" exception.您需要按顺序打开和处理文件,否则您将收到此“内部流位置意外更改”异常。
Therefore, make sure to:因此,请确保:
OpenReadStream
on File1
, then fully process the contents of the file呼叫OpenReadStream
在File1
,然后完全处理该文件的内容OpenReadStream
on File2
, and so on只有这样,调用OpenReadStream
的File2
,依此类推
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.