agent: |
Q6GwB4XEmKvA7IjX1TxcEnforce S3 Bucket Read Protection using Public Access Block Settings
Enforce S3 Bucket Read Protection using Public Access Block Settings
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task strengthens data security by restricting public read access to specified AWS S3 buckets. It updates Block Public Access settings and ACLs, ensuring data confidentiality. This action aligns with security compliance standards to protect sensitive information.
inputs
outputs
import boto3
from botocore.exceptions import ClientError, NoCredentialsError, BotoCoreError
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
def disable_public_write_access(bucket_name):
"""
Disables public write access for a specified S3 bucket by updating Block Public Access settings and ACL.
"""
s3 = boto3.client('s3',aws_access_key_id=access_key,aws_secret_access_key=secret_key)
# Update Block Public Access settings to block public ACLs
try:
s3.put_public_access_block(
Bucket=bucket_name,
PublicAccessBlockConfiguration={
'BlockPublicAcls': True,
'IgnorePublicAcls': True,
'BlockPublicPolicy': True,
'RestrictPublicBuckets': True
}
)
print(f"Updated Block Public Access settings for '{bucket_name}'.")
except ClientError as e:
print(f"Failed to update Block Public Access settings for '{bucket_name}': {e}")
raise
try:
if bucket_name:
#bucket_name = 'your-bucket-name'
disable_public_write_access(bucket_name)
else:
print("Please provide a bucket name to restrict public access")
except NoCredentialsError:
print("No AWS credentials found. Please configure your credentials.")
except BotoCoreError as e:
print(f"An error occurred accessing AWS S3 service: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
copied