用于ASP.NET Core的AWSSDK S3

时间:2017-04-06 12:45:58

标签: c# amazon-s3 asp.net-core-mvc .net-core

我尝试使用AWSSDK for .NET Core将文件上传到Amazon S3,但我总是遇到如下异常:

  

写入操作失败,请参阅内部异常。

     

与服务器的连接异常终止

无论是连接新加坡(全球)还是北京(cn-north-1),它仍然存在问题。

有人能给我一些解决这个问题的建议吗?

这是版本和代码!

  

.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"
}

控制器

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
       });
}

0 个答案:

没有答案