-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3_functions.py
More file actions
155 lines (137 loc) · 4.45 KB
/
s3_functions.py
File metadata and controls
155 lines (137 loc) · 4.45 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import boto3
import json
import os
from boto3.s3.transfer import TransferConfig
from progress_percentage import ProgressPercentage
def s3_client():
s3 = boto3.client('s3')
return s3
def s3_resource():
s3 = boto3.resource('s3')
return s3
def create_bucket(bucket_name, location):
print(bucket_name)
return s3_client().create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
'LocationConstraint': location
}
)
def list_buckets():
return s3_client().list_buckets()
def delete_bucket(bucket_name):
bucket = s3_resource().Bucket(bucket_name)
bucket.object_versions.delete()
bucket.delete()
def create_bucket_policy(bucket_name):
bucket_policy = {
"Version":"2012-10-17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:*"],
"Resource": ["arn:aws:s3:::" + bucket_name + "/*"]
}
]
}
policy_string = json.dumps(bucket_policy)
return s3_client().put_bucket_policy(
Bucket=bucket_name,
Policy=policy_string
)
def update_bucket_policy(bucket_name):
bucket_policy = {
"Version": "2012-10-17",
"Statement":[
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:DeleteObject",
"s3:GetObject",
"s3:PutObject"
],
"Resource":"arn:aws:s3:::" + bucket_name + '/*'
}
]
}
policy_string = json.dumps(bucket_policy)
return s3_client().put_bucket_policy(
Bucket=bucket_name,
Policy = policy_string
)
def get_bucket_policy(bucket_name):
return s3_client().get_bucket_policy(Bucket=bucket_name)
def server_side_encrypt_bucket(bucket_name):
return s3_client().put_bucket_encryption(
Bucket=bucket_name,
ServerSideEncryptionConfiguration={
"Rules":[
{
"ApplyServerSideEncryptionByDefault":{
"SSEAlgorithm": "AES256"
}
}
]
}
)
def get_bucket_encryption(bucket_name):
return s3_client().get_bucket_encryption(Bucket=bucket_name)
def upload_small_file(bucket_name, file_name):
file_path = os.path.dirname(__file__) + "\\resources\\" + file_name
return s3_client().upload_file(file_path, bucket_name, file_name)
def upload_large_file(bucket_name, file_name):
config = TransferConfig(multipart_threshold=1024 * 25, max_concurrency=10,
multipart_chunksize=1024 * 25, use_threads=True)
file_path = os.path.dirname(__file__) + "\\resources\\" + file_name
key_path = "multipart_files/" + file_name
s3_resource().meta.client.upload_file(file_path, bucket_name, key_path,
ExtraArgs={"ACL": "public-read", "ContentType":"application/x-ms-installer"},
Config=config,
Callback=ProgressPercentage(file_path))
def read_object_from_bucket(bucket_name, file_name):
return s3_client().get_object(
Bucket=bucket_name,
Key=file_name
)
def version_bucket_files(bucket_name):
s3_client().put_bucket_versioning(
Bucket=bucket_name,
VersioningConfiguration={
"Status": "Enabled"
}
)
def put_lifecycle_policy(bucket_name):
lifecycle_policy = {
"Rules": [
{
"ID": "Move text file to Glacier",
"Prefix": "textfile",
"Status": "Enabled",
"Transitions": [
{
"Date": "2019-01-01T00:00:00.000Z",
"StorageClass": "GLACIER"
}
]
},
{
"ID": "Move old versions to Glacier",
"Status": "Enabled",
"Prefix": "",
"NoncurrentVersionTransitions": [
{
"NoncurrentDays": 2,
"StorageClass": "GLACIER"
}
]
}
]
}
s3_client().put_bucket_lifecycle_configuration(
Bucket=bucket_name,
LifecycleConfiguration=lifecycle_policy
)