forked from ScottyLabs/cmumaps-data-acquisitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3_utils.py
More file actions
248 lines (205 loc) · 7.68 KB
/
Copy paths3_utils.py
File metadata and controls
248 lines (205 loc) · 7.68 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from minio import Minio
from dotenv import load_dotenv
import os
import json
load_dotenv()
access_key = os.getenv("S3_ACCESS_KEY")
secret_key = os.getenv("S3_SECRET_KEY")
s3_endpoint = os.getenv("S3_ENDPOINT")
# Create client with access and secret key.
client = Minio(
s3_endpoint,
access_key=access_key,
secret_key=secret_key,
)
bucket_name = "cmumaps"
def upload_json_file(local_file_path, s3_object_name):
"""Upload a JSON file to S3 bucket"""
try:
# Upload the file
client.fput_object(
bucket_name,
s3_object_name,
local_file_path,
content_type="application/json",
)
print(f"Successfully uploaded {local_file_path} as {s3_object_name}")
return True
except Exception as e:
print(f"Error uploading {local_file_path}: {e}")
return False
def upload_folder(local_folder_path, s3_folder_name, file_type="octet-stream"):
"""
Upload a folder to S3 bucket
Args:
local_folder_path (str): The local object name/path
s3_folder_name (str): The S3 object name/path
file_type (str): the file type
"""
try:
# Upload each of the files in the folder
for filename in os.listdir(local_folder_path):
local_path = os.path.join(local_folder_path, filename)
if os.path.isdir(local_path): # only upload files in the folder
continue
s3_object_name = f"{s3_folder_name}/{filename}"
client.fput_object(
bucket_name,
s3_object_name,
local_path,
content_type=f"application/{file_type}",
)
print(f"Successfully uploaded {local_folder_path} as {s3_folder_name}")
return True
except Exception as e:
print(f"Error uploading {local_folder_path}: {e}")
return False
def upload_generic_file(local_file_path, s3_object_name, file_type="octet-stream"):
"""Upload a JSON file to S3 bucket
Args:
local_file_path (str): The local object name/path
s3_object_name (str): The S3 object name/path
file_type (str): the file type
"""
try:
# Upload the file
client.fput_object(
bucket_name,
s3_object_name,
local_file_path,
content_type=f"application/{file_type}",
)
print(f"Successfully uploaded {local_file_path} as {s3_object_name}")
return True
except Exception as e:
print(f"Error uploading {local_file_path}: {e}")
def save_upload_json_file(
s3_object_name: str,
json_data: dict | list,
local_file_path: str = None,
indent: int = 2,
ensure_ascii: bool = False,
cleanup_local: bool = False,
) -> bool:
"""
Save JSON data to a local file and upload it to S3 bucket
Args:
s3_object_name (str): The object name/path in S3 bucket
json_data (dict | list): The JSON data to save and upload
local_file_path (str, optional): Local file path. If None, uses s3_object_name
indent (int): Number of spaces for JSON indentation (default: 2)
ensure_ascii (bool): If True, escape non-ASCII characters (default: False)
cleanup_local (bool): If True, delete local file after successful upload (default: False)
Returns:
bool: True if successful, False otherwise
"""
# Use s3_object_name as local path if not specified
local_path = local_file_path if local_file_path else s3_object_name
try:
# Save JSON data to local file with proper formatting
with open(local_path, "w", encoding="utf-8") as f:
json.dump(json_data, f, indent=indent, ensure_ascii=ensure_ascii)
print(f"Successfully saved JSON data to {local_path}")
except (IOError, OSError) as e:
print(f"Error saving file {local_path}: {e}")
return False
except (TypeError, ValueError) as e:
print(f"Error serializing JSON data: {e}")
return False
try:
# Upload the file to S3
success = upload_json_file(local_path, s3_object_name)
if success and cleanup_local and local_path != s3_object_name:
# Clean up local file if requested and it's not the same as S3 name
try:
os.remove(local_path)
print(f"Cleaned up local file {local_path}")
except OSError as e:
print(f"Warning: Could not delete local file {local_path}: {e}")
return success
except Exception as e:
print(f"Error uploading {local_path} to S3: {e}")
return False
def list_bucket_objects():
"""List all objects in the bucket"""
try:
objects = client.list_objects(bucket_name, recursive=True)
print(f"\nObjects in bucket '{bucket_name}':")
for obj in objects:
print(f" - {obj.object_name} ({obj.size} bytes)")
except Exception as e:
print(f"Error listing objects: {e}")
def download_json_file(s3_object_name, local_file_path):
"""Download a JSON file from S3 bucket"""
try:
# Download the file
client.fget_object(bucket_name, s3_object_name, local_file_path)
print(f"Successfully downloaded {s3_object_name} to {local_file_path}")
return True
except Exception as e:
print(f"Error downloading {s3_object_name}: {e}")
return False
def get_json_from_s3(s3_object_name, return_data=False):
"""
Get JSON data from S3 bucket
Args:
s3_object_name (str): The S3 object name/path
return_data (bool): If True, return the JSON data as Python object
If False, return the raw response object
Returns:
dict/list: JSON data if return_data=True, otherwise response object
"""
try:
# Get the object
response = client.get_object(bucket_name, s3_object_name)
if return_data:
# Read and parse JSON data
json_data = json.loads(response.read().decode("utf-8"))
response.close()
print(f"Successfully retrieved JSON data from {s3_object_name}")
return json_data
else:
print(f"Successfully retrieved object {s3_object_name}")
return response
except Exception as e:
print(f"Error getting {s3_object_name}: {e}")
return None
def get_generic_file_from_s3(s3_object_name):
"""
Get generic data from S3 bucket
Args:
s3_object_name (str): The S3 object name/path
return_data (bool): If True, return the data as Python object
If False, return the raw response object
Returns:
response object
"""
try:
# Get the object
response = client.get_object(bucket_name, s3_object_name)
print(f"Successfully retrieved object {s3_object_name}")
return response
except Exception as e:
print(f"Error getting {s3_object_name}: {e}")
return None
def list_json_files():
"""List all JSON files in the bucket"""
try:
objects = client.list_objects(bucket_name, recursive=True)
json_files = []
for obj in objects:
if obj.object_name.endswith(".json"):
json_files.append(
{
"name": obj.object_name,
"size": obj.size,
"last_modified": obj.last_modified,
}
)
print(f"\nJSON files in bucket '{bucket_name}':")
for file_info in json_files:
print(f" - {file_info['name']} ({file_info['size']} bytes)")
return json_files
except Exception as e:
print(f"Error listing JSON files: {e}")
return []