我得到的错误是:
The thread 0xffc has exited with code 0 (0x0).
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in Anjuna1.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware:Error: An unhandled exception has occurred: The write operation failed, see inner exception.
System.IO.IOException: The write operation failed, see inner exception. --->
System.Net.Http.WinHttpException: The connection with the server was
terminated abnormally
--- End of inner exception stack trace ---
此处完整的堆栈跟踪:https://pastebin.com/BGny5ULG
代码(.net core 2)如下:
private async Task S3UploadFileAsync(string fileName, Stream stream)
{
using (IAmazonS3 client = new AmazonS3Client(Amazon.RegionEndpoint.EUWest2))
{
var uploadRequest = new TransferUtilityUploadRequest
{
InputStream = stream,
BucketName = "******-*****",
CannedACL = S3CannedACL.AuthenticatedRead,
Key = fileName
};
TransferUtility fileTransferUtility = new TransferUtility(client);
try
{
await fileTransferUtility.UploadAsync(uploadRequest);
}
catch (System.Exception e)
{
throw e;
}
}
}
上述代码在我的本地计算机 (Windows 10) 上运行时运行良好,但在 EC2 实例 (Windows Server 2016) 上运行时运行不正常。
我曾尝试公开 S3 存储桶,认为这是一个权限问题(仍然无法正常工作)。
我认为这可能是 Windows 防火墙或 EC"实例安全组的问题,所以我安装了 AWS CLI 并可以使用它毫无问题地上传到 S3 实例。
无论我上传什么,我都尝试过大文件、小文件甚至空文件。
捕获到异常大约需要 30 秒,所以感觉好像超时了。将代码更改为不存在的 BucketName 会导致相同的症状,这让我认为这与存储桶配置无关(我已将名称加星号)。
如果您对尝试的建议有任何帮助,我们将不胜感激 - 我整天都在做这件事,但我的想法已经用完了……谢谢!
更新:此处的类似错误表明它与权限相关:https://matt40k.uk/2017/09/s3-errors/
最佳答案
我在使用 ASP.NET Core 2.0 应用程序将文件上传到 S3 时遇到了同样的问题,希望它对某人有所帮助。
在我的例子中,异常的原因是 AmazonS3Client 的构造函数中的区域参数不正确。
var amazonS3Client = new AmazonS3Client(awsCredentials, Amazon.RegionEndpoint.USEast1);
所以我同意问题作者的想法,即 AWS .NET SDK 没有提供格式正确的详细错误消息。
关于.net - 上传到 EC2 实例上的 S3 TransferUtility.UploadAsync 时为 "The connection with the server was terminated abnormally",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49116483/