import os import boto3 from botocore.exceptions import ClientError import logging # Configure silent logging for boto3 and botocore logging.getLogger('boto3').setLevel(logging.CRITICAL) logging.getLogger('botocore').setLevel(logging.CRITICAL) logging.getLogger('s3transfer').setLevel(logging.CRITICAL) logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) def upload_file_to_s3(file_path, bucket_name, s3_key): """ Upload a file to an S3 bucket silently. """ access_key = os.environ.get('AWS_ACCESS_KEY_ID') secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY') region = os.environ.get('AWS_REGION', 'us-east-1') if not access_key or not secret_key: return False s3_client = boto3.client( 's3', aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region ) try: # Extra arguments for public read if needed, but the user didn't specify. # Given the bucket name, it might be for a web app. s3_client.upload_file(file_path, bucket_name, s3_key) return True except ClientError: return False except Exception: return False def upload_job_artifacts(directory, job_id): """ Upload all generated clips and metadata for a job to S3. """ bucket_name = os.environ.get('AWS_S3_BUCKET', 'openshorts.app-clips') if not os.path.exists(directory): return for filename in os.listdir(directory): # Upload .mp4 clips and the metadata JSON if (filename.endswith(".mp4") or filename.endswith(".json")) and not filename.startswith("temp_"): file_path = os.path.join(directory, filename) s3_key = f"{job_id}/{filename}" upload_file_to_s3(file_path, bucket_name, s3_key)