"""Interface for managing sound recording delivery uploads storage in s3.""" import io import os from typing import Optional import boto3 s3_client = boto3.client('s3') QA_ACCOUNT_ID = '437795906767' ENV_CONFIG = { 'dev': { 'bucket': 'qa-sr-delivery-upload-bucket', 'prefix': '', 'account_id': QA_ACCOUNT_ID, }, 'qa': { 'bucket': 'qa-sr-delivery-upload-bucket', 'prefix': '', 'account_id': QA_ACCOUNT_ID, } } def write_sr_delivery_upload_xml( file_bytes: bytes, subdir: str, step_function_execution_id: str, service: Optional[str] = None): # noqa:E501 """Upload sound recording delivery DDEX XML data to QA bucket. Args: file_bytes: The file to transfer subdir: The subdirectory to store the file in step_function_execution_id: The execution ID of the step function service: The service name (e.g., 'youtube'). This will create a subfolder with the service name. Returns: bool: if files were all successfully put on S3 """ (bucket, prefix, account_id) = _config() put_response = s3_client.put_object( Bucket=bucket, Key=f'{prefix}{service + "/" if service else ""}{step_function_execution_id}/{subdir}', # noqa:E501 Body=io.BytesIO(file_bytes), ExpectedBucketOwner=account_id ) return put_response def _config(): """Get config based on value of Environment var.""" env = os.environ['ENVIRONMENT'] config = ENV_CONFIG[env] return ( config['bucket'], config['prefix'], config['account_id'] )