将文件放入私有子网中的 S3 存储桶中

putting a file in S3 bucket in private subnet

过去两天我一直在为这个问题苦苦挣扎。我正在尝试将文件放入私有子网中的 S3 存储桶中。下面是我的代码:

var s3Config = new AmazonS3Config() { ServiceURL = "https://myVPCnameamazonaws.com" };
            using (var cli = new AmazonS3Client(
               awsAccessKey,
               awsSecretKey,
               s3Config))

            {
                PutObjectRequest req = new PutObjectRequest()
                {
                    BucketName = "test",
                    ContentType = "image/jpg",
                    InputStream = st,
                    Key = fileName
                    
                };
                var response = cli.PutObject(req);

在我的代码的最后一行,我收到一条错误消息:HttpErrorResponseException:远程服务器返回错误:(403) 禁止访问。 内部异常:

WebException:远程服务器返回错误:(403) Forbidden。

我不确定我应该写什么代码来消除这个错误。我正在使用 AWS 工具包。 Visual studio 2019..net 框架 4.7.2

如有任何帮助,我们将不胜感激。

问题似乎是 AccessDenied 响应,表明您的请求已到达 S3,但您的凭据无权将对象上传到该 S3 存储桶。

检查与您的 AWS 凭证关联的 IAM 策略。他们有必要的 S3 权限吗?

一个适当的政策应该包括这样的东西(假设你的存储桶名称是 mybucket):

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "s3:PutObject",
            "s3:PutObjectAcl"
         ],
         "Resource":"arn:aws:s3:::mybucket/*"
      }
   ]
}