'boto3: How to delete a specific bucket policy
I'm working on a bucket that has 3 policies:
'Policy': '{"Version":"2012-10-17","Statement":[{"Sid":"RestrictGetObject","Effect":"Deny","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::bucket-name/*"},{"Sid":"AddTLS1.0Restriction","Effect":"Deny","Principal":"*","Action":"*","Resource":"arn:aws:s3:::bucket-name/*","Condition":{"NumericEquals":{"s3:TlsVersion":"1.0"}}},{"Sid":"AddTLS1.1Restriction","Effect":"Deny","Principal":"*","Action":"*","Resource":"arn:aws:s3:::bucket-name/*","Condition":{"NumericEquals":{"s3:TlsVersion":"1.1"}}}]}'}
I only want to remove the "Sid":"RestrictGetObject" policy, but boto3 function delete_bucket_policy() doesn't take any arguments to specify the policy. It seems to delete all buckets.
So far, my script is like this:
def delete_bucket_policy(bucket):
bucket_policy = {
'Version': '2012-10-17',
'Statement': [{
'Sid': 'RestrictGetObject',
'Effect': 'Deny',
'Principal': '*',
'Action': ['s3:GetObject'],
'Resource': f'arn:aws:s3:::{bucket}/*'
}]
}
# Convert the policy from JSON dict to string
bucket_policy = json.dumps(bucket_policy)
try:
response = s3client.delete_bucket_policy(Bucket=bucket)
logger.info('Bucket: {}, Policy: {}'.format(bucket, response))
except ClientError as e:
logger.info('Bucket: {}, Error: {}'.format(bucket, e))
I haven't run it because i'm almost certain delete_bucket_policy() will delete ALL policies.
So, how can I delete ONLY the policy I want?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
