bucket.py 1020 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import boto3
  2. import json
  3. import os
  4. def connect():
  5. access_key = os.environ["AWS_ACCESS_KEY_ID"]
  6. secret_key = os.environ["AWS_SECRET_ACCESS_KEY"]
  7. return boto3.client('s3', 'us-east-1',
  8. endpoint_url="https://s3.jibby.org",
  9. aws_access_key_id = access_key,
  10. aws_secret_access_key = secret_key)
  11. def create_bucket(bucket_name):
  12. conn = connect()
  13. conn.create_bucket(Bucket=bucket_name)
  14. def set_public_read_policy(bucket_name):
  15. bucket_policy = {
  16. "Version":"2012-10-17",
  17. "Statement":[
  18. {
  19. "Sid":"AddPerm",
  20. "Effect":"Allow",
  21. "Principal": "*",
  22. "Action":["s3:GetObject", "s3:ListBucket"],
  23. "Resource":[
  24. "arn:aws:s3:::{0}/*".format(bucket_name),
  25. "arn:aws:s3:::{0}".format(bucket_name),
  26. ]
  27. }
  28. ]
  29. }
  30. bucket_policy = json.dumps(bucket_policy)
  31. conn = connect()
  32. conn.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy)