himanshu208

Untitled

Oct 20th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import boto3
  2. import json
  3. s3 = boto3.resource("s3")
  4. s3client = boto3.client('s3')
  5. bucket_name = "121999347-bucket"
  6. bucket_config = {'LocationConstraint': 'ap-southeast-2'}
  7.  
  8. data = 'Here we have some data for the files to create folders'
  9. f= open("dummyfile","w+")
  10. f.write(data)
  11. f.close()
  12.  
  13. try:
  14. # s3.create_bucket(Bucket=bucket_name,CreateBucketConfiguration=bucket_config)
  15. # response = s3.meta.client.upload_file("dummyfile", bucket_name,'folder1/file.txt')
  16. # response = s3.meta.client.upload_file("dummyfile", bucket_name,'folder2/file.txt')
  17. # response = s3.meta.client.upload_file("dummyfile", bucket_name,'folder3/file.txt')
  18. # print "files added"
  19.  
  20. bucket_policy = {
  21. "Version": "2012-10-17",
  22. "Statement": [
  23. {
  24. "Sid": "Stmt1540041485474",
  25. "Action": "s3:*",
  26. "Effect": "Deny",
  27. "Resource": "arn:aws:s3:::121999347-bucket/folder1/*",
  28. "Condition": {
  29. "StringNotEquals": {
  30. "aws:username": "21999347@student.uwa.edu.au"
  31. }
  32. },
  33. "Principal": "*"
  34. },
  35. {
  36. "Action": "s3:*",
  37. "Effect": "Allow",
  38. "Resource": "arn:aws:s3:::121999347-bucket/folder2/*",
  39. "Condition": {
  40. "StringLike": {
  41. "aws:username": "*@folder2.uwa.edu.au"
  42. }
  43. },
  44. "Principal": "*",
  45. },
  46. {
  47. "Action": "s3:*",
  48. "Effect": "Allow",
  49. "Resource": "arn:aws:s3:::121999347-bucket/folder3/*",
  50. "Resource": "arn:aws:s3:::121999347-bucket/folder3/",
  51. "Resource": "arn:aws:s3:::121999347-bucket/folder3",
  52. "Condition": {
  53. "StringLike": {
  54. "aws:username": "*@folder3.uwa.edu.au"
  55. }
  56. },
  57. "Principal": "*"
  58. }
  59. ]
  60. }
  61.  
  62. bucket_policy = json.dumps(bucket_policy)
  63. s3client.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy)
  64.  
  65.  
  66. except Exception as e:
  67. print(e)
Add Comment
Please, Sign In to add comment