简体   繁体   English

如何通过 AWS CLI 更新 Amazon S3 存储桶策略?

[英]How to update an Amazon S3 Bucket Policy via the AWS CLI?

I have a requirement to add a new line "arn:aws:sts::1262767:assumed-role/EC2-support-services" to an Amazon S3 bucket policy.我需要向 Amazon S3 存储桶策略添加新行“arn:aws:sts::1262767:assumed-role/EC2-support-services”。

Something like this:像这样的东西:

Before:前:

{
  "Version":"2012-10-17",
  "Statement":[
    {
      "Sid":"AddCannedAcl",
      "Effect":"Allow",
    "Principal": {"AWS": ["arn:aws:iam::111122223333:root","arn:aws:iam::444455556666:root"]},
      "Action":["s3:PutObject","s3:PutObjectAcl"],
      "Resource":"arn:aws:s3:::awsexamplebucket1/*",
      "Condition":{
     "StringNotLike": {
        "aws:arn": [
          "arn:aws:sts::1262767:assumed-role/GR_COF_AWS_Prod_Support/*"
        ]
      }       
     }
   
    }
  ]
}

After:后:

{
  "Version":"2012-10-17",
  "Statement":[
    {
      "Sid":"AddCannedAcl",
      "Effect":"Allow",
    "Principal": {"AWS": ["arn:aws:iam::111122223333:root","arn:aws:iam::444455556666:root"]},
      "Action":["s3:PutObject","s3:PutObjectAcl"],
      "Resource":"arn:aws:s3:::awsexamplebucket1/*",
      "Condition":{
     "StringNotLike": {
        "aws:arn": [
          "arn:aws:sts::1262767:assumed-role/GR_COF_AWS_Prod_Support/*",
           "arn:aws:sts::1262767:assumed-role/EC2-support-services"
        ]
      }       
     }
   
    }
  ]
}

What is the AWS CLI command I need to use to add this line?我需要使用什么 AWS CLI 命令来添加此行?

To do this you would need to override the existing bucket policy using the put-bucket-policy command as there is no versioning.为此,您需要使用put-bucket-policy命令覆盖现有存储桶策略,因为没有版本控制。

An example of running this would be the below command运行它的一个例子是下面的命令

aws s3api put-bucket-policy --bucket MyBucket --policy file://policy.json

By storing the current and new policies as JSON files you can switch between the commands if you need to rollback by updating the filename in the --policy argument.通过将当前和新策略存储为 JSON 文件,如果您需要通过更新--policy参数中的文件名来回滚,您可以在命令之间切换。

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

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