List All AWS S3 Buckets

This task involves retrieving and displaying a comprehensive list of all Amazon S3 buckets within an AWS account. This step is crucial as it provides a clear overview of all the storage resources available, serving as a starting point for various management and security tasks, such as enforcing encryption or implementing access policies. By generating a list of all S3 buckets, users can easily identify and manage their storage resources, ensuring effective organization and security compliance within their AWS environment.

import boto3 from botocore.exceptions import BotoCoreError, NoCredentialsError, PartialCredentialsError creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] def list_all_s3_buckets(): try: # Creating a Boto3 S3 client s3 = boto3.client('s3',aws_access_key_id=access_key,aws_secret_access_key=secret_key) # Sending a request to list S3 buckets response = s3.list_buckets() # Extracting bucket names from the response all_buckets = [bucket['Name'] for bucket in response['Buckets']] return all_buckets except NoCredentialsError: # Handle the exception when credentials are not found print("Error: AWS credentials not found") return None except PartialCredentialsError: # Handle the exception when provided credentials are incomplete print("Error: Incomplete AWS credentials") return None except BotoCoreError as e: # Handle other Boto3 core exceptions print(f"Error: AWS SDK for Python (Boto3) core error occurred - {e}") return None except Exception as e: # Handle any other general exceptions print(f"Unexpected error: {e}") return None # Main block buckets = list_all_s3_buckets() if buckets is not None: if buckets: print("Found the following S3 buckets:") for bucket in buckets: print(bucket) else: print("No S3 buckets found.") else: print("Error occurred while trying to list S3 buckets.") # Use Create S3 bucket task if you need to create a S3 bucket for CloudTrail logging context.skip_sub_tasks = True
copied