forked from opengisch/QFieldCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_storage_bucket.py
83 lines (58 loc) · 1.99 KB
/
create_storage_bucket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import boto3
# TODO: add cli params
# TODO: add non-delete bucket policy
def load_env_file():
"""Read env file and return a dict with the variables"""
environment = {}
with open("../.env") as f:
for line in f:
if line.strip():
splitted = line.rstrip().split("=", maxsplit=1)
if len(splitted) == 2:
environment[splitted[0]] = splitted[1]
return environment
def _get_s3_bucket(env, bucket_name):
"""Get S3 Bucket according to the env variable
STORAGE_BUCKET_NAME"""
# Create session
session = boto3.Session(
aws_access_key_id=env["STORAGE_ACCESS_KEY_ID"],
aws_secret_access_key=env["STORAGE_SECRET_ACCESS_KEY"],
region_name=env["STORAGE_REGION_NAME"],
)
# Get the bucket objects
s3 = session.resource("s3", endpoint_url=env["STORAGE_ENDPOINT_URL"])
return s3.Bucket(bucket_name)
def _get_s3_client(env):
s3_client = boto3.client(
"s3",
region_name=env["STORAGE_REGION_NAME"],
aws_access_key_id=env["STORAGE_ACCESS_KEY_ID"],
aws_secret_access_key=env["STORAGE_SECRET_ACCESS_KEY"],
endpoint_url=env["STORAGE_ENDPOINT_URL"],
)
return s3_client
def _create_bucket(env, bucket_name):
client = _get_s3_client(env)
client.create_bucket(
Bucket=bucket_name,
)
def _enable_versioning(env, bucket_name):
client = _get_s3_client(env)
client.put_bucket_versioning(
Bucket=bucket_name, VersioningConfiguration={"Status": "Enabled"}
)
def _print_access_control_list(env, bucket_name):
# Retrieve a bucket's ACL
s3 = _get_s3_client(env)
result = s3.get_bucket_acl(Bucket=bucket_name)
from pprint import pprint
pprint(result)
env = load_env_file()
# _print_access_control_list(env, 'qfieldcloud-test')
# _create_bucket(env, 'qfieldcloud-dev')
# bucket.objects.all().delete()
# bucket.object_versions.all().delete()
# client.delete_bucket(
# Bucket='test-bucket',
# )