-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmultiple_buckets.py
More file actions
executable file
·83 lines (76 loc) · 2.65 KB
/
multiple_buckets.py
File metadata and controls
executable file
·83 lines (76 loc) · 2.65 KB
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
from botocore.exceptions import ClientError
from botocore.exceptions import ParamValidationError
import sys
session = boto3.session.Session()
# configuration for this connection
# in a "real" script, these values would probably not be hard-coded
configuration = {
"endpoint_url": "[endpoint_url_here]",
"access_key": "[access_key_here]",
"secret_key": "[secret_key_here]",
"buckets": {
"web-assets",
"form-submissions"
}
}
# create our s3c session using the variables above
# note "use_ssl=False", as outlined in the accompanying article
s3c = session.client(
aws_access_key_id=configuration["access_key"],
aws_secret_access_key=configuration["secret_key"],
endpoint_url=configuration["endpoint_url"],
service_name="s3",
use_ssl=False,
)
# setup our bucket lifecycle policy
# please use this carefully as these settings will need to be
# altered before use outside this demo environment
lifecycle_policy = {
"Rules": [
{
"Expiration": {"Days": 10},
"Filter": {"Prefix": "demo"},
"Status": "Enabled",
"ID": "ExpiryPolicy-10Days-ObjectPrefix-demo",
},
{
"Expiration": {"Days": 30},
"Filter": {"Prefix": "object"},
"Status": "Enabled",
"ID": "ExpiryPolicy-1Month-ObjectPrefix-object",
}
]
}
# iterate over the bucket list, verify that each one does
# not already exist and, if not, attempt to create it
for bucket in configuration["buckets"]:
pass
# check if bucket exists
try:
s3c.head_bucket(Bucket=bucket)
print(f"Bucket exists : {bucket}")
except ClientError:
print(f"Bucket {bucket} does not exist. "
+ "Attempting to create bucket ...")
try:
s3c.create_bucket(Bucket=bucket)
except Exception as err:
print("An exception occurred while creating the "
+ f"{bucket} bucket. "
+ f"Details: {err}")
sys.exit()
# bucket either already exists or we were able to create it
# now apply the lifecycle policy
try:
print(f"Applying lifecycle policy to {bucket} bucket ...")
s3c.put_bucket_lifecycle_configuration(Bucket=bucket,
LifecycleConfiguration=lifecycle_policy)
except s3c.exceptions.NoSuchBucket:
print("Bucket does not exist.")
except ParamValidationError as err:
print("The provided lifecycle policy is invalid. Please check "
+ "your policy configuration. Details:")
print(f"{err}")
except Exception as err:
print(err)