"""Module that handles storage operations.""" from botocore.exceptions import ConnectionError from transcoding.constants import exceptions as transcoding_exceptions from transcoding.models import s3_file def upload_job_results(source_path, transcoding_job_details): """Copy file from source_path to final destination. Args: source_path (str): Source file path. transcoding_job_details (dict): Transcoding job details. Returns: dict: Upload details. Raises: TranscodingRetryableError: In case of retryable error like connection issue. TranscodingFatalError: In case of fatal error. """ try: output_bucket = transcoding_job_details['output_bucket'] output_key = transcoding_job_details['output_key'] return s3_file.upload_s3_file(source_path, output_bucket, output_key) except ConnectionError as e: raise transcoding_exceptions.TranscodingRetryableError(str(e)) except Exception as e: raise transcoding_exceptions.TranscodingFatalError(str(e)) def copy_object(source_bucket, source_key, destination_bucket, destination_key): """Copy file from source bucket to destination bucket. Args: source_bucket (str): Source S3 bucket. source_key (str): Source S3 bucket key. destination_bucket (str): Destination S3 bucket. destination_key (str): Destination S3 bucket key. Raises: TranscodingRetryableError: In case of retryable error like connection issue. TranscodingFatalError: In case of fatal error. """ try: s3_file.copy_s3_file( source_bucket, source_key, destination_bucket, destination_key) except ConnectionError as e: raise transcoding_exceptions.TranscodingRetryableError(str(e)) except Exception as e: raise transcoding_exceptions.TranscodingFatalError(str(e)) def generate_url_for_uploaded_asset( bucket, key, client_method='get_object', expires_in=86400): """Generate a presigned url for uploaded asset. Args: bucket (str): Bucket name. key (str): Existing S3 object key. client_method (str): Client method for operation by result URL. expires_in (int): The number of seconds the presigned url is valid for. By default it expires in 24 hours (86400 seconds). Returns: str: Presigned url. """ return s3_file.create_presigned_url(bucket, key, client_method, expires_in)