@@ -138,26 +138,31 @@ def check_service_health(service_urls, access_token):
138
138
})
139
139
return health_status
140
140
141
- def upload_file_to_s3 (file_name , bucket_name , object_name = None ):
141
+
142
+ def upload_json_to_s3 (json_data , bucket_name , object_name ):
142
143
"""
143
- Upload a file to an S3 bucket.
144
+ Upload JSON data to an S3 bucket.
144
145
145
146
Parameters:
146
- - file_name (str ): File to upload.
147
+ - json_data (dict ): JSON data to upload.
147
148
- bucket_name (str): Bucket to upload to.
148
- - object_name (str): S3 object name. If not specified, file_name is used.
149
+ - object_name (str): S3 object name.
149
150
"""
150
- # If S3 object_name was not specified, use file_name
151
- if object_name is None :
152
- object_name = file_name
153
-
154
151
# Create an S3 client
155
152
s3_client = boto3 .client ('s3' )
156
153
157
154
try :
158
- # Upload the file
159
- response = s3_client .upload_file (file_name , bucket_name , object_name )
160
- return True , "File uploaded successfully."
155
+ # Convert the JSON data to a string
156
+ json_string = json .dumps (json_data )
157
+
158
+ # Upload the JSON string to S3
159
+ response = s3_client .put_object (
160
+ Bucket = bucket_name ,
161
+ Key = object_name ,
162
+ Body = json_string ,
163
+ ContentType = 'application/json'
164
+ )
165
+ return True , "JSON uploaded successfully."
161
166
except Exception as e :
162
167
# The upload failed; return False and the error
163
168
return False , str (e )
@@ -205,14 +210,11 @@ def lambda_handler(event, context):
205
210
now = datetime .datetime .now ()
206
211
filename = now .strftime ("health_check_%Y-%m-%d_%H-%M-%S.json" )
207
212
208
- # Write the result to a JSON file
209
- with open (filename , 'w' ) as file :
210
- json .dump (health_status , file , indent = 4 )
211
-
212
- # Upload the file to S3
213
- bucket_name = 'mgmt-13l4zrzw'
214
- upload_message = upload_file_to_s3 (filename , bucket_name )
215
- print ("Upload Status:" , upload_message )
213
+ # Upload the JSON data to S3
214
+ bucket_name = 'mgmt-13l4zrzw'
215
+ upload_status , upload_message = upload_json_to_s3 (health_status , bucket_name , filename )
216
+ print ("Upload Status:" , upload_message )
217
+
216
218
217
219
# Output the result as JSON
218
220
return {
0 commit comments