title: AWS Lambda function in .NET throwing error when creating new file for s3 bucket (Could not determine content length)
body: <p>I am developing an AWS lambda function usign .NET core 2. This is my first time developing AWS lambda function using .NET. What I am trying to do now is so simple, read the existing file and save the file with new name (new name). Reading the file is working fine. But when I save the file using PutObjectRequest, it is giving me an error. </p>
  
  <p>This is my code for the handler function</p>
  
  <pre><code>public async Task&lt;string&gt; FunctionHandler(S3Event evnt, ILambdaContext context)
        {
           var s3Event = evnt.Records?[0].S3;
           if(s3Event == null)
           {
              return null;
           }
  
           try
           {
              if(s3Event.Object.Key.ToLower().Contains("thumb"))
              {
                 //Console.WriteLine("The image is already a thumb file");
                 return "The file is aready a thumb image file";
              }
  
              string[] pathSegments = s3Event.Object.Key.Split('/');
              string eventCode = pathSegments[0];
              string userEmail = pathSegments[1];
              string filename = pathSegments[2];
              string extension = Path.GetExtension(filename); //.jpeg with "dot"
              string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
  
              using(var objectResponse = await this.S3Client.GetObjectAsync(s3Event.Bucket.Name, s3Event.Object.Key))
              {
                 using(Stream responseStream = objectResponse.ResponseStream)
                 {
                    //This code is throwing error
                    await this.S3Client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
                    {
                       BucketName = s3Event.Bucket.Name,
                       Key = fileNameWithoutExtension + ".thumb" + extension,
                       InputStream = responseStream
                    });
                 }
              }
  
              return "Thumbnail version of the image has been created";
  
           }
           catch(Exception e)
           {
              context.Logger.LogLine($"Error getting object {s3Event.Object.Key} from bucket {s3Event.Bucket.Name}. Make sure they exist and your bucket is in the same region as this function.");
              context.Logger.LogLine(e.Message);
              context.Logger.LogLine(e.StackTrace);
              throw;
           }
        }
  </code></pre>
  
  <p>When I run the test function, it is giving me this error.</p>
  
  <pre><code>{
    "errorType": "AggregateException",
    "errorMessage": "One or more errors occurred. (Could not determine content length)",
    "stackTrace": [
      "at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)",
      "at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
    ],
    "cause": {
      "errorType": "AmazonS3Exception",
      "errorMessage": "Could not determine content length",
      "stackTrace": [
        "at Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshaller.GetStreamWithLength(Stream baseStream, Int64 hintLength)",
        "at Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshaller.Marshall(PutObjectRequest putObjectRequest)",
        "at Amazon.Runtime.Internal.Marshaller.PreInvoke(IExecutionContext executionContext)",
        "at Amazon.Runtime.Internal.Marshaller.InvokeAsync[T](IExecutionContext executionContext)",
        "at Amazon.Runtime.Internal.CallbackHandler.&lt;InvokeAsync&gt;d__9`1.MoveNext()",
        "--- End of stack trace from previous location where exception was thrown ---",
        "at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
        "at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
        "at Amazon.S3.Internal.AmazonS3ExceptionHandler.&lt;InvokeAsync&gt;d__1`1.MoveNext()",
        "--- End of stack trace from previous location where exception was thrown ---",
        "at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
        "at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
        "at Amazon.Runtime.Internal.ErrorCallbackHandler.&lt;InvokeAsync&gt;d__5`1.MoveNext()",
        "--- End of stack trace from previous location where exception was thrown ---",
        "at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
        "at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
        "at Amazon.Runtime.Internal.MetricsHandler.&lt;InvokeAsync&gt;d__1`1.MoveNext()",
        "--- End of stack trace from previous location where exception was thrown ---",
        "at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
        "at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
        "at AwsS3Lambda.Function.&lt;FunctionHandler&gt;d__10.MoveNext() in C:\\Users\\Acer\\Desktop\\MementoCSharp\\memento-dotnet\\AwsS3Lambda\\Function.cs:line 99"
      ]
    },
    "causes": [
      {
        "errorType": "AmazonS3Exception",
        "errorMessage": "Could not determine content length",
        "stackTrace": [
          "at Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshaller.GetStreamWithLength(Stream baseStream, Int64 hintLength)",
          "at Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshaller.Marshall(PutObjectRequest putObjectRequest)",
          "at Amazon.Runtime.Internal.Marshaller.PreInvoke(IExecutionContext executionContext)",
          "at Amazon.Runtime.Internal.Marshaller.InvokeAsync[T](IExecutionContext executionContext)",
          "at Amazon.Runtime.Internal.CallbackHandler.&lt;InvokeAsync&gt;d__9`1.MoveNext()",
          "--- End of stack trace from previous location where exception was thrown ---",
          "at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
          "at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
          "at Amazon.S3.Internal.AmazonS3ExceptionHandler.&lt;InvokeAsync&gt;d__1`1.MoveNext()",
          "--- End of stack trace from previous location where exception was thrown ---",
          "at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
          "at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
          "at Amazon.Runtime.Internal.ErrorCallbackHandler.&lt;InvokeAsync&gt;d__5`1.MoveNext()",
          "--- End of stack trace from previous location where exception was thrown ---",
          "at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
          "at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
          "at Amazon.Runtime.Internal.MetricsHandler.&lt;InvokeAsync&gt;d__1`1.MoveNext()",
          "--- End of stack trace from previous location where exception was thrown ---",
          "at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
          "at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
          "at AwsS3Lambda.Function.&lt;FunctionHandler&gt;d__10.MoveNext() in C:\\Users\\Acer\\Desktop\\MementoCSharp\\memento-dotnet\\AwsS3Lambda\\Function.cs:line 99"
        ]
      }
    ]
  }
  </code></pre>
  
  <p>What is wrong and how can I solve it? How can I save a new file?</p>