How to change permission recursively to folder with AWS s3 or AWS s3api

后端 未结 7 1378
情歌与酒
情歌与酒 2020-12-06 02:28

I am trying to grant permissions to an existing account in s3.

The bucket is owned by the account, but the data was copied from another account\'s bucket.

Wh

7条回答
  •  眼角桃花
    2020-12-06 02:48

    use python to set up the permissions recursively

    #!/usr/bin/env python
    import boto3
    import sys
    
    client = boto3.client('s3')
    BUCKET='enter-bucket-name'
    
    def process_s3_objects(prefix):
        """Get a list of all keys in an S3 bucket."""
        kwargs = {'Bucket': BUCKET, 'Prefix': prefix}
        failures = []
        while_true = True
        while while_true:
          resp = client.list_objects_v2(**kwargs)
          for obj in resp['Contents']:
            try:
                print(obj['Key'])
                set_acl(obj['Key'])
                kwargs['ContinuationToken'] = resp['NextContinuationToken']
            except KeyError:
                while_true = False
            except Exception:
                failures.append(obj["Key"])
                continue
    
        print "failures :", failures
    
    def set_acl(key):
      client.put_object_acl(     
        GrantFullControl="id=%s" % get_account_canonical_id,
        Bucket=BUCKET,
        Key=key
    )
    
    def get_account_canonical_id():
      return client.list_buckets()["Owner"]["ID"]
    
    
    process_s3_objects(sys.argv[1])
    

提交回复
热议问题