-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3_bucket_operations.py
66 lines (55 loc) · 1.84 KB
/
s3_bucket_operations.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
# coding: utf-8
import boto3
from botocore.exceptions import NoCredentialsError, ParamValidationError
from botocore.errorfactory import ClientError
def create_new_bucket():
s3 = boto3.client('s3')
s3.create_bucket(
Bucket='mooc-courses-certificates',
CreateBucketConfiguration={'LocationConstraint': 'eu-north-1'}
)
def upload_file_to_bucket():
s3 = boto3.client('s3')
file_to_upload = '/Users/neeraj/Desktop/Codeacademy_HTML_Certificate.pdf'
bucket_name = 'mooc-courses-certificates'
object_key = 'certificates/Codeacademy_HTML_Certificate1.pdf'
try:
s3.upload_file(
file_to_upload,
bucket_name,
object_key
)
except NoCredentialsError as e:
print('AWS Credentials not found, please configure S3 credentials.', e)
except ParamValidationError as e:
print('Input parameters invalid', e)
except ClientError as e:
if 'NoSuchBucket' in e.response:
print('Bucket does not exists.')
def copy_object():
s3 = boto3.client('s3')
src_bucket ='mooc-courses-certificates'
object_to_copy = f'{src_bucket}/certificates/Codeacademy_HTML_Certificate.pdf'
object_key = 'courses/Codeacademy-HTML.pdf'
dest_bucket = 'www.125systems.com'
s3.copy_object(
Bucket=dest_bucket,
Key=object_key,
CopySource=object_to_copy
)
def move_object():
"""There is no move operation on boto3 client.
Copy the object and then delete the source."""
def delete_object():
bucket_name = 'mooc-courses-certificates'
object_key = 'certificates/Codeacademy_HTML_Certificate1.pdf'
s3 = boto3.client('s3')
s3.delete_object(
Bucket=bucket_name,
Key=object_key
)
if __name__ == "__main__":
# copy_object()
# upload_file_to_bucket()
# move_object()
delete_object()