简体   繁体   English

在 AWS Lambda 中获取“内部流位置意外更改”

[英]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:因此,请确保:

  • Call OpenReadStream on File1 , then fully process the contents of the file呼叫OpenReadStreamFile1 ,然后完全处理该文件的内容
  • Only then, call OpenReadStream on File2 , and so on只有这样,调用OpenReadStreamFile2 ,依此类推

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有人有幸让 .NET 6 REST API 项目在 AWS Z04A7DA3C5B05CAD85DA1EEBB923 中工作? - Has anyone had any luck getting a .NET 6 REST API project to work in an AWS Lambda? 权限意外更改 - Unexpectedly changed permissions 如何使用 c# 在 aws lambda 中 stream 文件 - How to stream a file in aws lambda using c# .net 核心从 controller 返回的响应中的一些内容已意外更改 - .net core some content in response returned from controller has been changed unexpectedly HTTPS请求意外更改为HTTP - HTTPS request unexpectedly changed to HTTP Moq Lambda表达式表现异常 - Moq lambda expression behaving unexpectedly 具有内部Lambda表达的Lambda表达 - Lambda Expression with Inner Lambda Expression HttpPostedFileBase流上传到AWS开发工具包存储桶中没有数据 - HttpPostedFileBase Stream Upload to AWS SDK Bucket has no Data 即使数据发生变化,实时数据库也能获得相同的结果 - Getting same results with realtime database even when data has changed 为什么没有捕获的lambda从C#5中的静态变为C#6中的实例方法? - Why has a lambda with no capture changed from a static in C# 5 to an instance method in C# 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM