"""Call for check_s3_file_exists to check if s3 file exists.""" from boto.exception import S3ResponseError from oto import response from assets.connectors import s3 from assets.connectors import sentry from assets.models.exceptions.s3_file_not_found import S3FileNotFound def check_s3_file_exists(bucket_name, file_key): """Generate download url for provided file in s3 bucket. Args: bucket_name (str): name of bucket where file is stored. file_key (str): path to file for which url should be generated. Returns: Response: Response with success or error. """ s3_connection = s3.connect_to_s3() try: bucket = s3_connection.get_bucket(bucket_name) file = bucket.get_key(file_key) if not file: raise S3FileNotFound(key=file_key, bucket=bucket_name) return response.Response({'status': 'ok'}) except S3ResponseError as ex: if sentry.sentry_client: sentry.sentry_client.captureException() return response.create_error_response( code=ex.error_code, message=ex.message, status=ex.status) except S3FileNotFound as ex: if sentry.sentry_client: sentry.sentry_client.captureException() return response.create_not_found_response( message=ex.args[0]) def rename_s3_file(bucket_name, old_path, new_path): """Move an object in S3 from one key to another within the same bucket. Args: bucket_name (str): name of the bucket where the file is stored. old_path (str): current path of the file in s3. new_path (str): desired new path for the file in s3. Returns: Response: Response with success or error. """ s3_connection = s3.connect_to_s3() try: bucket = s3_connection.get_bucket(bucket_name) file = bucket.get_key(old_path) if not file: return response.create_not_found_response() file.copy(bucket_name, new_path) file.delete() except S3ResponseError as ex: sentry.sentry_client.captureException() return response.create_fatal_response(message=ex.message) return response.Response()