Skip to content

Commit

Permalink
Add bucket exists check before uploading to S3 (#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
shloka-bhalgat-unskript authored May 31, 2024
1 parent 20aab8f commit 8efda84
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions unskript-ctl/unskript_upload_results_to_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def __init__(self):
logger.debug("AWS credentials are not set in environment variables")
return

self.bucket_name = 'lightbeam-reports'
self.uglobals = UnskriptGlobals()

try:
Expand All @@ -48,6 +47,23 @@ def __init__(self):
def create_s3_folder_path(self):
# Initialize folder_exists
folder_exists = False

# Ensure the bucket exists
try:
self.s3_client.head_bucket(Bucket=self.bucket_name)
logger.debug(f"S3 bucket {self.bucket_name} exists")
except ClientError as e:
if e.response['Error']['Code'] == '404':
logger.debug(f"S3 bucket {self.bucket_name} does not exist, creating bucket")
try:
self.s3_client.create_bucket(Bucket=self.bucket_name)
logger.debug(f"S3 bucket {self.bucket_name} created")
except ClientError as e:
logger.debug(f"Failed to create bucket: {e}")
return False # Exit if the bucket cannot be created
else:
logger.debug(f"Error checking bucket existence: {e}")
return False # Exit if there is any other error

# Ensure the folder structure exists in the bucket
try:
Expand All @@ -69,6 +85,8 @@ def create_s3_folder_path(self):
except ClientError as e:
logger.debug(f"Failed to create folder: {e}")

return True

def rename_and_upload_failed_objects(self, checks_output):
try:
# Convert checks_output to JSON format
Expand All @@ -86,7 +104,9 @@ def rename_and_upload_failed_objects(self, checks_output):
logger.debug(f"Failed to write JSON data to local file: {e}")
return

self.create_s3_folder_path()
if not self.create_s3_folder_path():
logger.debug("Unable to create bucket")
return

# Upload the JSON file
try:
Expand All @@ -102,7 +122,9 @@ def rename_and_upload_failed_objects(self, checks_output):
os.remove(self.local_file_name)

def rename_and_upload_other_items(self):
self.create_s3_folder_path()
if not self.create_s3_folder_path():
logger.debug("Unable to create bucket")
return
# Upload the files in the CURRENT_EXECUTION_RUN_DIRECTORY
file_list_to_upload = [self.local_file_name]
if self.uglobals.get('CURRENT_EXECUTION_RUN_DIRECTORY') and \
Expand Down

0 comments on commit 8efda84

Please sign in to comment.