forked from Seagate/cortx-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.py
42 lines (31 loc) · 895 Bytes
/
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
import os
import boto3
import settings
s3 = boto3.resource(
"s3",
endpoint_url=os.environ.get('endpoint_url'),
aws_access_key_id=os.environ.get('aws_access_key_id'),
aws_secret_access_key=os.environ.get('aws_secret_access_key'),
)
bucket = s3.Bucket(os.environ.get('bucket_name'))
def download(filename):
try:
output_path = './.tmp/{}'.format(filename)
bucket.download_file(filename, output_path)
return output_path
except Exception as e:
return None
def upload(input_path,filename):
try:
bucket.upload_file(input_path, filename)
return input_path
except Exception as e:
return None
def list_files():
try:
files = []
for my_bucket_object in bucket.objects.all():
files.append(my_bucket_object.key)
return files
except Exception as e:
return []