"""AWS S3 related utility functions.""" import csv from boto.exception import S3ResponseError import boto3 from oto import response import smart_open from prs import config from prs.connectors import sentry from prs.constants import error from prs.constants import s3 from prs.constants import tools_message def write_csv_to_s3(s3_key, data): """Write data to csv file on S3. Args: s3_key (str): String of format '/' data (dict): data of format {'columns': [id,name], 'rows': [(1, name1), (2,name2)]} """ try: source_url = s3.FILE_URL.format( bucket_name=config.PRS_BUCKET_NAME, file_path=s3_key) with smart_open.smart_open(source_url, 'wb') as fileobj: writer = csv.writer(fileobj) writer.writerow(data['columns']) writer.writerows(data['rows']) return response.Response( message=tools_message.CSV_GENERATION_SUCCESS_MESSAGE) 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 Exception as ex: if sentry.sentry_client: sentry.sentry_client.captureException() return response.create_error_response( code=error.ERROR_CODE_CSV_FILE_WRITE, message=str(ex) ) def get_presigned_url(s3_key): """Get presigned public URL for given S3 path. Args: s3_key (str): String of format '/' Returns: url (str): Returns public URL for given S3 path """ s3_client = boto3.client('s3') url = s3_client.generate_presigned_url( 'get_object', Params={'Bucket': config.PRS_BUCKET_NAME, 'Key': s3_key}, ExpiresIn=s3.SIGNED_URL_EXPIRATION_TIME) return url