forked from ro6ley/flask-drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_demo.py
39 lines (30 loc) · 848 Bytes
/
s3_demo.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
import boto3
def upload_file(file_name, bucket):
"""
Function to upload a file to an S3 bucket
"""
object_name = file_name
s3_client = boto3.client('s3')
response = s3_client.upload_file(file_name, bucket, object_name)
return response
def download_file(file_name, bucket):
"""
Function to download a given file from an S3 bucket
"""
s3 = boto3.resource('s3')
output = f"downloads/{file_name}"
s3.Bucket(bucket).download_file(file_name, output)
return output
def list_files(bucket):
"""
Function to list files in a given S3 bucket
"""
s3 = boto3.client('s3')
contents = []
try:
for item in s3.list_objects(Bucket=bucket)['Contents']:
print(item)
contents.append(item)
except Exception as e:
pass
return contents