Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import boto3
- import json
- s3 = boto3.resource("s3")
- s3client = boto3.client('s3')
- bucket_name = "121999347-bucket"
- bucket_config = {'LocationConstraint': 'ap-southeast-2'}
- data = 'Here we have some data for the files to create folders'
- f= open("dummyfile","w+")
- f.write(data)
- f.close()
- try:
- s3.create_bucket(Bucket=bucket_name,CreateBucketConfiguration=bucket_config)
- response = s3.meta.client.upload_file("dummyfile", bucket_name,'folder1/file.txt')
- response = s3.meta.client.upload_file("dummyfile", bucket_name,'folder2/file.txt')
- response = s3.meta.client.upload_file("dummyfile", bucket_name,'folder3/file.txt')
- print "files added"
- bucket_policy = {
- "Version": "2012-10-17",
- "Statement": [
- {
- "Action": "s3:*",
- "Effect": "Deny",
- "Resource": "arn:aws:s3:::121999347-bucket/folder1",
- "Condition": {
- "StringNotEquals": {
- "aws:username": "21999347@student.uwa.edu.au"
- }
- },
- "Principal": {
- "AWS": "arn:aws:iam::032418238795:root"
- }
- },
- {
- "Action": "s3:*",
- "Effect": "Deny",
- "Resource": "arn:aws:s3:::121999347-bucket/folder2",
- "Condition": {
- "StringNotLike": {
- "aws:userid": "@folder2.uwa.edu.au"
- }
- },
- "Principal": {
- "AWS": "arn:aws:iam::032418238795:root"
- }
- },
- {
- "Action": "s3:*",
- "Effect": "Deny",
- "Resource": "arn:aws:s3:::121999347-bucket/folder3",
- "Condition": {
- "StringNotLike": {
- "aws:username": "@folder3.uwa.edu.au"
- }
- },
- "Principal": "*"
- }
- ]
- }
- bucket_policy = json.dumps(bucket_policy)
- s3client.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy)
- except Exception as e:
- print(e)
Add Comment
Please, Sign In to add comment