简体   繁体   English

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

[英]putting a file in S3 bucket in private subnet

I am struggling with this issue for past two days.过去两天我一直在努力解决这个问题。 I am trying to put a file in S3 bucket in the private subnet.我正在尝试将文件放入私有子网的 S3 存储桶中。 Below is my code:下面是我的代码:

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

In the last line of my code, I get an error saying : HttpErrorResponseException: The remote server returned an error: (403) Forbidden.在我的代码的最后一行中,我收到一条错误消息:HttpErrorResponseException:远程服务器返回错误:(403) Forbidden。 Inner Exception:内部异常:

WebException: The remote server returned an error: (403) Forbidden. WebException:远程服务器返回错误:(403) Forbidden。

在此处输入图片说明

I am not sure what code should I write to get rid of this error.我不确定我应该写什么代码来摆脱这个错误。 I am using AWS tool Kit.我正在使用 AWS 工具包。 Visual studio 2019..net framework 4.7.2 Visual Studio 2019..net 框架 4.7.2

Any help with this will be greatly appreciated.对此的任何帮助将不胜感激。

The problem appears to be an AccessDenied response which suggests that your request reached S3 but your credentials do not have permission to upload an object to that S3 bucket.问题似乎是 AccessDenied 响应,它表明您的请求已到达 S3,但您的凭据无权将对象上传到该 S3 存储桶。

Check the IAM policies associated with your AWS credentials.检查与您的 AWS 凭证关联的 IAM 策略。 Do they have the necessary S3 permissions?他们是否拥有必要的 S3 权限?

An appropriate policy would include something like this (assuming your bucket name is mybucket ):适当的策略将包括如下内容(假设您的存储桶名称是mybucket ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM