def get_env_var(name: str) -> str: env_var = os.getenv(name) if env_var is None: error_message = f'{name} Enviroment Variable is missing for this lambda' logger.error(error_message) raise Exception(error_message) return env_var
def create_presigned_post(bucket_name, object_name, fields=None, conditions=None, expiration=3600): """Generate a presigned URL S3 POST request to upload a file :param bucket_name: string :param object_name: string :param fields: Dictionary of prefilled form fields :param conditions: List of conditions to include in the policy :param expiration: Time in seconds for the presigned URL to remain valid :return: Dictionary with the following keys: url: URL to post to fields: Dictionary of form fields and values to submit with the POST :return: None if error. """ # Generate a presigned S3 POST URL s3_client = boto3.client('s3') try: response = s3_client.generate_presigned_post(bucket_name, object_name, Fields=fields, Conditions=conditions, ExpiresIn=expiration) except ClientError as e: logger.error(e) return None # The response contains the presigned URL and required fields return response
def _build_error_response(err, status: HTTPStatus = HTTPStatus.INTERNAL_SERVER_ERROR ) -> dict: logger.error(str(err)) return { 'statusCode': status, 'headers': { 'Content-Type': 'application/json' }, 'body': str(err), }
def upload_s3_object_presigned(source_file: str, object_name: str, bucket_name: str) -> str: response = create_presigned_post(bucket_name, object_name) if response is None: raise Exception( "Error generating presigned url for {object_name=}, {bucket_name=}" ) with open(source_file, 'rb') as f: files = {'file': (object_name, f)} http_response = requests.post(response['url'], data=response['fields'], files=files) # If successful, returns HTTP status code 204 logger.info(f'File upload HTTP status code: {http_response.status_code}') if http_response.status_code > 300: logger.error( f"Error uploading object with presigned url {http_response.content=}" ) raise Exception(f"Error uploading {object_name=} to {bucket_name=}")