himanshu208

Untitled

Oct 20th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 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. "Action": "s3:*",
  25. "Effect": "Deny",
  26. "Resource": "arn:aws:s3:::121999347-bucket/folder1",
  27. "Condition": {
  28. "StringNotEquals": {
  29. "aws:username": "21999347@student.uwa.edu.au"
  30. }
  31. },
  32. "Principal": {
  33. "AWS": "arn:aws:iam::032418238795:root"
  34. }
  35. },
  36. {
  37. "Action": "s3:*",
  38. "Effect": "Deny",
  39. "Resource": "arn:aws:s3:::121999347-bucket/folder2",
  40. "Condition": {
  41. "StringNotLike": {
  42. "aws:userid": "@folder2.uwa.edu.au"
  43. }
  44. },
  45. "Principal": {
  46. "AWS": "arn:aws:iam::032418238795:root"
  47. }
  48. },
  49. {
  50. "Action": "s3:*",
  51. "Effect": "Deny",
  52. "Resource": "arn:aws:s3:::121999347-bucket/folder3",
  53. "Condition": {
  54. "StringNotLike": {
  55. "aws:username": "@folder3.uwa.edu.au"
  56. }
  57. },
  58. "Principal": "*"
  59. }
  60. ]
  61. }
  62.  
  63. bucket_policy = json.dumps(bucket_policy)
  64. s3client.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy)
  65.  
  66.  
  67. except Exception as e:
  68. print(e)
Add Comment
Please, Sign In to add comment