25
loading...
This website collects cookies to deliver better user experience
def list_bucket_policies():
"""
This function lists all policies attached to s3 bucket.
:return: None
"""
s3_client = boto3.client("s3")
try:
response = s3_client.get_bucket_policy(Bucket="testbucket-frompython-2")
pprint(response)
except ClientError as e:
# if you do not have any policy attached to bucket it will throw error
# An error occurred (NoSuchBucketPolicy) when calling the GetBucketPolicyStatus operation:
# The bucket policy does not exist
print("No policy attached to this bucket")
boto3.setup_default_session(profile_name='PROFILE_NAME_FROM_YOUR_MACHINE')
s3 = boto3.client("s3",
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
def check_bucket_status():
"""
This function checks if bucket has public access ot private access.
:return: None
"""
s3_client = boto3.client("s3")
try:
response = s3_client.get_bucket_policy_status(Bucket="testbucket-frompython-2")
pprint(response["PolicyStatus"])
except ClientError as e:
# if you do not have any policy attached to bucket it will throw error
# An error occurred (NoSuchBucketPolicy) when calling the GetBucketPolicyStatus operation:
# The bucket policy does not exist
print("No policy attached to this bucket")
#sample output of above code
{'IsPublic': False}
def set_bucket_policy():
"""
This function adds policy to bucket.
:return: None
"""
# policy for making all objects in bucket public by default
public_policy = """{
"Id": "Policy1577423306792",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1577423305093",
"Action": "s3:*",
"Effect": "Allow",
"Resource": "arn:aws:s3:::testbucket-frompython-2/*",
"Principal": {
"AWS": [
"*"
]
}
}
]
}"""
s3_client = boto3.client("s3")
try:
response = s3_client.put_bucket_policy(
Bucket="testbucket-frompython-2", Policy=public_policy
)
pprint(response)
# checking bucket status. This should show us s3 bucket is public now
check_bucket_status()
except ClientError as e:
# if you do not have any policy attached to bucket it will throw error
# An error occurred (NoSuchBucketPolicy) when calling the GetBucketPolicyStatus operation:
# The bucket policy does not exist
print(e)
def delete_bucket_policies():
"""
This function deletes all policies attached to the bucket.
:return: None
"""
s3_client = boto3.client("s3")
s3_client.delete_bucket_policy(Bucket="testbucket-frompython-2")
# checking bucket policies. This should say no policies attached to this bucket.
check_bucket_status()