@@ -263,7 +263,7 @@ class CustomConfigs:
263
263
264
264
_custom_configs : list [CustomConfig ] = None
265
265
_aws_bucket_name : str = None
266
- _aws_bucket_path : str = "/ aws_openai/lambda_openai_function/custom_configs/"
266
+ _aws_bucket_path : str = "aws_openai/lambda_openai_function/custom_configs/"
267
267
_aws_bucket_path_validated : bool = False
268
268
269
269
def __init__ (self , config_path : str = None , aws_s3_bucket_name : str = None ):
@@ -285,21 +285,14 @@ def __init__(self, config_path: str = None, aws_s3_bucket_name: str = None):
285
285
s3 = settings .aws_session .resource ("s3" )
286
286
bucket = s3 .Bucket (self ._aws_bucket_name )
287
287
288
- for obj in bucket .objects .filter (Prefix = self ._aws_bucket_path ):
289
- i += 1
290
- file_content = obj .get ()["Body" ].read ().decode ("utf-8" )
291
- config_json = yaml .safe_load (file_content )
292
- custom_config = CustomConfig (config_json = config_json , index = i )
293
- self ._custom_configs .append (custom_config )
294
-
295
- def list_yaml_files (bucket_name ):
296
- """List all the YAML files in the AWS S3 bucket"""
297
- s3 = settings .aws_session .resource ("s3" )
298
- bucket = s3 .Bucket (bucket_name )
299
-
300
- for obj in bucket .objects .all ():
301
- if obj .key .endswith (".yaml" ) or obj .key .endswith (".yml" ):
302
- print ("Found YAML file:" , obj .key )
288
+ for obj in bucket .objects .filter (Prefix = self .aws_bucket_path ):
289
+ if obj .key .endswith (".yaml" ) or obj .key .endswith (".yml" ):
290
+ i += 1
291
+ file_content = obj .get ()["Body" ].read ().decode ("utf-8" )
292
+ config_json = yaml .safe_load (file_content )
293
+ if config_json :
294
+ custom_config = CustomConfig (config_json = config_json , index = i )
295
+ self ._custom_configs .append (custom_config )
303
296
304
297
@property
305
298
def valid_configs (self ) -> list [CustomConfig ]:
@@ -311,38 +304,36 @@ def invalid_configs(self) -> list[CustomConfig]:
311
304
"""Return a list of invalid configs"""
312
305
return [config for config in self ._custom_configs if not config .is_valid ]
313
306
307
+ @property
308
+ def aws_bucket_path (self ) -> str :
309
+ """Return the remote host"""
310
+ return self ._aws_bucket_path
311
+
314
312
@property
315
313
def aws_bucket_path_validated (self ) -> bool :
316
314
"""Return True if the remote host is valid"""
317
315
return self ._aws_bucket_path_validated
318
316
319
- @property
320
- def aws_bucket_full_path (self ) -> str :
321
- """Return the remote host"""
322
- if self .aws_bucket_path_validated :
323
- return self ._aws_bucket_name + self ._aws_bucket_path
324
- return None
325
-
326
317
def verify_bucket (self , bucket_name : str ):
327
318
"""Verify that the remote host is valid"""
319
+ if not bucket_name :
320
+ return
321
+
328
322
s3 = settings .aws_session .resource ("s3" )
329
- bucket = s3 .Bucket (bucket_name )
330
- folder_path = self ._aws_bucket_path
331
323
try :
332
324
# Check if bucket exists
333
325
s3 .meta .client .head_bucket (Bucket = bucket_name )
334
326
# pylint: disable=broad-exception-caught
335
- except Exception :
327
+ except Exception as e :
328
+ log .warning ("Bucket %s does not exist: %s" , bucket_name , e )
336
329
return
337
330
338
- try :
339
- # Create any missing folders
340
- if not any (s3_object .key .startswith (folder_path ) for s3_object in bucket .objects .all ()):
341
- s3 .Object (bucket_name , folder_path ).put ()
342
- self ._aws_bucket_path_validated = True
343
- # pylint: disable=broad-exception-caught
344
- except Exception :
345
- pass
331
+ # Create any missing folders
332
+ bucket = s3 .Bucket (bucket_name )
333
+ if not any (s3_object .key .startswith (self .aws_bucket_path ) for s3_object in bucket .objects .all ()):
334
+ print (f"Creating folder { self .aws_bucket_path } in bucket { bucket_name } " )
335
+ s3 .Object (bucket_name , self .aws_bucket_path ).put ()
336
+ self ._aws_bucket_path_validated = True
346
337
347
338
def to_json (self ) -> json :
348
339
"""Return the _custom_configs list as a JSON object"""
@@ -370,4 +361,9 @@ def custom_configs(self) -> CustomConfigs:
370
361
return self ._custom_configs
371
362
372
363
373
- config = SingletonCustomConfigs ().custom_configs .valid_configs
364
+ _custom_configs = SingletonCustomConfigs ().custom_configs
365
+ config = _custom_configs .valid_configs
366
+ if len (_custom_configs .invalid_configs ) > 0 :
367
+ invalid_configurations = list (_custom_configs .invalid_configs .file_name )
368
+ invalid_configurations = ", " .join (invalid_configurations )
369
+ log .error ("Invalid custom config files: %s" , invalid_configurations )
0 commit comments