I have tried to upload file to Amazon S3 using AWSSDK for .NET Core, but I always got exception like below:
The write operation failed, see inner exception.
The connection with the server was terminated abnormally
No matter connect to Singapore (Global) or Beijing (cn-north-1), it still has problem.
Can anyone give me some suggestions to solve this problem?
This is version and codes, please!
.NET Core / 1.1.1
AWSSDK.S3 / 3.3.5.10
Startup.cs
// Add framework services.
services.AddMvc();
services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
services.AddAWSService<IAmazonS3>();
appsettings.json
"AWS_ACCESS_KEY_ID": "xxxxxxxxxx",
"AWS_SECRET_ACCESS_KEY": "xxxxxxxxxxxxxxxxxxxxxxxx",
"AWS": {
"Region": "cn-north-1",
"ServiceURL": "https://s3.cn-north-1.amazonaws.com.cn"
}
Controller
private readonly IAmazonS3 _AmazonS3Client;
public UsersController(
IAmazonS3 AmazonS3Client)
{
_AmazonS3Client = AmazonS3Client;
}
[HttpPost]
[Route("users/picture/upload")]
public async Task<IActionResult> UploadUserPhoto(string UserId)
{
Amazon.S3.Model.PutObjectResponse response = await
_AmazonS3Client.PutObjectAsync(
new Amazon.S3.Model.PutObjectRequest()
{
BucketName = "mybucket",
Key = fileName,
InputStream = uploadFiles[0].OpenReadStream(),
CannedACL = S3CannedACL.PublicRead
});
}