boto3 s3 api failing with "(NoSuchBucketPolicy) when calling the GetBucketPolicyStatus operation"

Viewed 728

I am playing around with boto3 for the first time. I want to loop over an array that I have populated with s3 buckets and then get the policy status for each bucket though I am getting an error and not sure why. It works for the first index of the array but errors out after that. why?

#!/usr/bin/python3

import boto3

all_buckets=[]

session = boto3.Session(profile_name='default')
s3 = session.client('s3')

def get_bucket_list():
    for i in s3.list_buckets()['Buckets']:
        all_buckets.append(f'  {i["Name"]}')

get_bucket_list()
for j in all_buckets:
    k = (j.strip())
    policy = s3.get_bucket_policy_status(Bucket=k)
    print(policy)

Error: (Note: the references to AWS ID's are not real)

{'ResponseMetadata': {'RequestId': 'D237HE2EPLFX78KV', 'HostId': 'A23zqZFjzk2qeqkPRn0ano3KBiatr9YoPVB94EFGfh0T/ojbNbOkAyz82hibmijgVox3vTrfGz=', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amz-id-2': 'A23zqZFjzk2qeqkPRn0ano3KBiatr9YoPVB94EFGfh0T/ojbNbOkAyz82hibmijgVox3vTrfGz=', 'x-amz-request-id': 'D237HE2EPLFX78KV', 'date': 'Thu, 04 Mar 2021 00:06:33 GMT', 'transfer-encoding': 'chunked', 'server': 'AmazonS3'}, 'RetryAttempts': 0}, 'PolicyStatus': {'IsPublic': False}}
Traceback (most recent call last):
  File "./test2.py", line 17, in <module>
    policy = s3.get_bucket_policy_status(Bucket=k)
  File "/usr/lib/python3/dist-packages/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/lib/python3/dist-packages/botocore/client.py", line 635, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (NoSuchBucketPolicy) when calling the GetBucketPolicyStatus operation: The bucket policy does not exist
1 Answers

GetBucketPolicy throws exception when there is no policy attached.

So, we should catch the exception because not every bucket needs to have a bucket policy. Access to bucket can be controlled via bucket policy or IAM policy.

Here is the modified code:

def get_bucket_list():
    for i in s3.list_buckets()['Buckets']:
        all_buckets.append(f'  {i["Name"]}')

get_bucket_list()
for j in all_buckets:
    k = (j.strip())
    try:
      policy = s3.get_bucket_policy_status(Bucket=k)
      print(policy)
    except s3.exceptions.from_code('NoSuchBucketPolicy'): 
      print("No Bucket Policy for bucket " + k)
    except: 
      print('something else failed')
Related