aws-sdk-net: TransferUtility.UploadAsync() throws error: "content would exceed Content-Length" when update to .NET8

Describe the bug

I recently migrated our project to .NET8 and updated all the packages to latest version, but I keep getting a strange error when I try to upload a stream to Amazon S3 storage. I try to upload a relatively small file ~500 KB.

Expected Behavior

Should not throw exception in at System.IO.Stream.<CopyToAsync>

Current Behavior

It throws exception:

System.Net.Http.HttpRequestException: Unable to write content to request stream; content would exceed Content-Length.
   at System.IO.Stream.<CopyToAsync>g__Core|27_0(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
   at System.Net.Http.HttpContent.<CopyToAsync>g__WaitAsync|56_0(ValueTask copyTask)
   at System.Net.Http.HttpConnection.SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.SendRequestContentWithExpect100ContinueAsync(HttpRequestMessage request, Task`1 allowExpect100ToContinueTask, HttpContentWriteStream stream, Timer expect100Timer, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken)
   at Amazon.Runtime.Internal.HttpHandler`1.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.RedirectHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.S3.Internal.AmazonS3ResponseHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.Signer.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.S3.Internal.AmazonS3ExceptionHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.S3.Transfer.Internal.SimpleUploadCommand.ExecuteAsync(CancellationToken cancellationToken)
   at STP.BlobStorage.Aws.AwsS3Repo.UploadAsync(Stream sourceStream, String fullPath, String contentType, CancellationToken cancellationToken

Reproduction Steps

public async Task UploadAsync(Stream sourceStream, string fullPath, string contentType, CancellationToken cancellationToken = default(CancellationToken))
{
    Guard.ValidateFullPath(fullPath, "fullPath");
    try
    {
        TransferUtility transferUtility = new TransferUtility(_s3Client);
        await transferUtility.UploadAsync(sourceStream, _bucketName, fullPath, cancellationToken);
        _logger.LogDebug("Uploaded stream to {fullPath}", fullPath);
    }
    catch (AmazonServiceException ex3)
    {
        AmazonServiceException ex2 = ex3;
        _logger.LogError(ex2, "Got AmazonServiceException in UploadAsync while uploading object to '{fullPath}'.", fullPath);
        throw new StorageProviderException($"Got AmazonServiceException in {"UploadAsync"} while uploading object to '{fullPath}'.", ex2);
    }
    catch (Exception ex4)
    {
        Exception ex = ex4;
        _logger.LogError(ex, "Got exception in UploadAsync while uploading object to '{fullPath}'.", fullPath);
        throw new StorageException($"Got exception in {"UploadAsync"} while uploading object to '{fullPath}'.", ex);
    }
}

Possible Solution

No response

Additional Information/Context

When I revert my project to .NET6 the issue disappears. If needed I can provide additional information

AWS .NET SDK and/or Package version used

AWSSDK.S3, Version=3.3.0.0

Targeted .NET Platform

.NET8

Operating System and version

Windows 11, Debian 12

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @julian-dimitroff,

Thanks for providing more details. Support for uploading unseekable streams in Transfer Utility was added in S3 (3.7.202.0). You may want to upgrade to this version to confirm that it’s an unseekable stream issue.

However, S3 (3.7.305.8) may have introduced some conflicts with the above feature release. Will discuss this with the team and keep you posted.

Regards, Chaitanya

@tedKarabeliov uploading unseekable streams will only work with the Transfer Utility, not the low-level API. If you want to use the low-level API you would have to write the logic for buffering each part in memory using the low-level UploadPart API which is already done for you if you use the Transfer Utility. See above message by @bhoradc for the version number where this support was added.

@julian-dimitroff As for your use case, I was able to replicate it using the sample code you provided and postman. Thanks for providing that, it made it very easy to validate against. Like @ashishdhingra stated, microsoft seems to be returning 0 on the length property of the stream in .NET 7+. This is causing us to set the content-length header using this information, and actually the exception gets thrown by the httpClient because we are trying to write more data than we said we would in the content-length header, hence the error message.

However, given this is blocking you guys, we will provide a fix shortly after some validation. Will post back here when the fix is out. Thanks for reporting the issue.

Looks like TransferUtility doesn’t support streams where CanSeek is false. https://jasonterando.medium.com/net-core-tee-streaming-and-buffered-s3-uploads-4a063230d99f