forked from GRDI-GenARCC/Maloja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3_actions.py
executable file
·56 lines (42 loc) · 1.69 KB
/
s3_actions.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
#!/bin/env python
import boto3
def upload_file_to_s3(bucket_name, local_file_path, s3_file_key):
"""
Upload a file to an Amazon S3 bucket.
Parameters:
bucket_name (str): The name of the S3 bucket to upload the file to.
local_file_path (str): The local file path of the file to be uploaded.
s3_file_key (str): The key or name of the file in the S3 bucket.
Returns:
bool: True if the upload is successful, False otherwise.
"""
try:
# Create an S3 client
s3 = boto3.client('s3')
# Upload the file to the S3 bucket
s3.upload_file(local_file_path, bucket_name, s3_file_key)
print(f"File uploaded successfully to s3://{bucket_name}/{s3_file_key}")
return True
except Exception as e:
print(f"Error uploading file to S3: {e}")
return False
def download_file_from_s3(bucket_name, s3_file_key, local_file_path):
"""
Download a single file from an Amazon S3 bucket.
Parameters:
bucket_name (str): The name of the S3 bucket.
s3_file_key (str): The key or name of the file in the S3 bucket.
local_file_path (str): The local file path to save the downloaded file.
Returns:
bool: True if the download is successful, False otherwise.
"""
try:
# Create an S3 client
s3 = boto3.client('s3')
# Download the file from the S3 bucket
s3.download_file(bucket_name, s3_file_key, local_file_path)
print(f"File downloaded successfully from s3://{bucket_name}/{s3_file_key} to {local_file_path}")
return True
except Exception as e:
print(f"Error downloading file from S3: {e}")
return False