-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3_uploader.py
More file actions
53 lines (35 loc) · 1.39 KB
/
s3_uploader.py
File metadata and controls
53 lines (35 loc) · 1.39 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
import os
import json
import dotenv
import boto3
class S3Uploader:
def __init__(self,name:str, s3_client:boto3.client, bucket_name:str):
self.name = name
self.s3_client = s3_client
self.bucket_name = bucket_name
def upload_file(self, file_path:str, s3_path:str):
try:
self.s3_client.upload_file(file_path, self.bucket_name, s3_path)
print(f"File {file_path} uploaded to {self.bucket_name}/{s3_path}")
except Exception as e:
print(f"Failed to upload {file_path} to {self.bucket_name}/{s3_path}: {e}")
def run(self):
for file in os.listdir(f"data/{self.name}"):
if file.endswith(".parquet"):
file_name = file.replace(".parquet", "")
self.upload_file(f"data/{self.name}/{file}", f"{self.name}/{file_name}/{file}")
def execute():
dotenv.load_dotenv('.env')
with open('config.json', 'r') as f:
configs = json.load(f)
s3_client = boto3.client(
's3',
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
region_name=os.getenv('AWS_REGION')
)
for config in configs.values():
s3_uploader = S3Uploader(config['name'], s3_client, os.getenv('S3_BUCKET_NAME'))
s3_uploader.run()
if __name__ == "__main__":
execute()