import boto3
# Replace 'YOUR_BUCKET_NAME' with the actual name of your S3 bucket
bucket_name = 'YOUR_BUCKET_NAME'
# Replace 'YOUR_AWS_REGION' with the AWS region where your bucket is located
region = 'YOUR_AWS_REGION'
# Replace 'YOUR_ACCESS_KEY' and 'YOUR_SECRET_ACCESS_KEY' with your AWS credentials
s3_client = boto3.client('s3',
region_name=region,
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_ACCESS_KEY')
try:
# Attempt to list the objects in the bucket
response = s3_client.list_objects_v2(Bucket=bucket_name)
print('Access to S3 bucket successful!')
print('Objects in the bucket:')
for obj in response['Contents']:
print(obj['Key'])
except Exception as e:
print('Access to S3 bucket denied. Error:', str(e))