Created
July 6, 2018 09:52
-
-
Save davidp94/cb2e34b76f5bb925d527ab728a0bc4ec to your computer and use it in GitHub Desktop.
Check read only and read-write permissions on S3 bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3, botocore | |
BUCKET_NAME = '' | |
READ_ONLY_ID = '' | |
READ_ONLY_KEY = '' | |
RW_ID = '' | |
RW_KEY = '' | |
# List objects in an Amazon S3 bucket | |
# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#id209 | |
client_ronly = boto3.resource('s3', aws_access_key_id=READ_ONLY_ID, aws_secret_access_key=READ_ONLY_KEY) | |
bucket_ronly = client_ronly.Bucket(BUCKET_NAME) | |
print('ronly list objects') | |
for obj in bucket_ronly.objects.all(): | |
print(obj.key) | |
try: | |
# try to push a file | |
bucket_ronly.put_object(Bucket=BUCKET_NAME, Key='yol', Body=b'42') | |
except botocore.exceptions.ClientError: | |
# Good | |
pass | |
# List objects in an Amazon S3 bucket | |
# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#id209 | |
client_rw = boto3.resource('s3', aws_access_key_id=RW_ID, aws_secret_access_key=RW_KEY) | |
bucket_rw = client_rw.Bucket(BUCKET_NAME) | |
print('rw list objects') | |
for obj in bucket_rw.objects.all(): | |
print(obj.key) | |
# push a file | |
bucket_rw.put_object(Bucket=BUCKET_NAME, Key='dumpit', Body=b'yo yo') | |
# reader should be able to see | |
print('ronly list objects') | |
for obj in bucket_ronly.objects.all(): | |
print(obj.key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment