.NET8迁移后Amazon S3流上传报错:内容超出Content-Length
.NET6迁移至.NET8后Amazon S3上传流出现Content-Length超出错误
将项目从.NET6迁移至.NET8并更新所有包至最新版本后,向Amazon S3存储上传流时持续抛出错误:System.Net.Http.HttpRequestException: Unable to write content to request stream; content would exceed Content-Length
其中STP.BlobStorage是无法获取源码的第三方包,上传请求由AmazonS3Client内部发起。
使用的NuGet包列表
<ItemGroup> <PackageReference Include="Asp.Versioning.Mvc" Version="8.0.0" /> <PackageReference Include="LEXolutionServiceBus" Version="1.6.55" /> <PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.3.1" /> <PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="7.3.1" /> <PackageReference Include="MongoDB.Driver" Version="2.23.1" /> <PackageReference Include="MongoDB.Driver.Core" Version="2.23.1" /> <PackageReference Include="OpenSearch.Client" Version="1.6.0" /> <PackageReference Include="Serilog.AspNetCore" Version="8.0.1" /> <PackageReference Include="Serilog.Enrichers.Environment" Version="2.3.0" /> <PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" /> <PackageReference Include="Serilog.Expressions" Version="4.0.0" /> <PackageReference Include="Serilog.Sinks.OpenSearch" Version="1.0.0" /> <PackageReference Include="STP.AspNetCore.StreamingExtensions" Version="3.0.0" /> <PackageReference Include="STP.BlobStorage" Version="2.0.0" /> <PackageReference Include="STP.UserManagement.Identity.TokenValidation" Version="6.0.2" /> <PackageReference Include="STP.Extensions.System" Version="6.0.0" /> </ItemGroup>
错误堆栈信息
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) --- End of inner exception stack trace --- at STP.BlobStorage.Aws.AwsS3Repo.UploadAsync(Stream sourceStream, String fullPath, String contentType, CancellationToken cancellationToken) at STP.IM.DocumentPoolService.Data.StorageManager.UploadContentStream(Guid tenantId, Guid id, String name, ITypedStream typedStream, CancellationToken cancellationToken) in C:\Projects\STP.IM.DocumentPool\STP.IM.DocumentPoolService\Data\StorageManager.cs:line 78 at STP.IM.DocumentPoolService.Manager.DocumentManager.UploadDocument(Guid tenantId, IDocumentReference document, String name, ITypedStream stream) in C:\Projects\STP.IM.DocumentPool\STP.IM.DocumentPoolService\Manager\DocumentManager.cs:line 250 at STP.IM.DocumentPoolService.Controller.DocumentController.UploadDocumentWithTenantId(Guid tenantId, Guid documentId, String name, CancellationToken cancellationToken) in C:\Projects\STP.IM.DocumentPool\STP.IM.DocumentPoolService\Controller\DocumentController.cs:line 389 at lambda_method34(Closure, Object) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|7_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
上传代码实现
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); } }
无法在请求发起时直接控制Content-Length参数,请问此次从.NET6到.NET8的迁移为何会引发该问题?
内容的提问来源于stack exchange,提问作者Yulian Dimitrov
相关产品推荐
相关产品推荐

