def event(event, context): """ Triggered by s3 events, object create and remove """ # Sample event: # # _event = {'Records': [{'eventVersion': '2.0', 'eventSource': 'aws:s3', 'awsRegion': 'us-east-1', # 'eventTime': '2017-11-25T23:57:38.988Z', 'eventName': 'ObjectCreated:Put', # 'userIdentity': {'principalId': 'AWS:AROAJWJG5IVL3URF4WKKK:su-xx-test-create'}, # 'requestParameters': {'sourceIPAddress': '75.82.111.45'}, # 'responseElements': {'x-amz-request-id': '9E39B8F9A3D22C83', # 'x-amz-id-2': 'GiWcmOHnxnxOJa64k5rkgTsiiwo+JOR3p2DvuQ6txQXl9jC0jNhO+gbDwwP/3WKAl4oPbVZsTE4='}, # 's3': {'s3SchemaVersion': '1.0', 'configurationId': 'dad7b639-0cd8-4e47-a2ae-91cc5bf866c8', # 'bucket': {'name': 'su-xx', 'ownerIdentity': {'principalId': 'AEZOG5WRKFUM2'}, # 'arn': 'arn:aws:s3:::su-xx'}, # 'object': {'key': 'test/bbc498ea-d23b-11e7-af42-2a31486da301', 'size': 11060, # 'eTag': 'd50cb2e8d7ad6768d46b3d47ba9b241e', # 'sequencer': '005A1A0372C5A1D292'}}}]} logger.debug('event: {}'.format(event)) event_name = event['Records'][0]['eventName'] key = event['Records'][0]['s3']['object']['key'] asset_id = key.replace('{}/'.format(os.environ['S3_KEY_BASE']), '') try: if 'ObjectCreated:Put' == event_name: try: asset = AssetModel.get(hash_key=asset_id) asset.mark_received() except UpdateError: return { 'statusCode': httplib.BAD_REQUEST, 'body': { 'error_message': 'Unable to update ASSET'} } elif 'ObjectRemoved:Delete' == event_name: try: asset = AssetModel.get(hash_key=asset_id) asset.delete() except DeleteError: return { 'statusCode': httplib.BAD_REQUEST, 'body': { 'error_message': 'Unable to delete ASSET {}'.format(asset) } } except DoesNotExist: return { 'statusCode': httplib.NOT_FOUND, 'body': { 'error_message': 'ASSET {} not found'.format(asset_id) } } return {'statusCode': httplib.ACCEPTED}
def update(event, context): logger.debug('event: {}'.format(event)) try: asset_id = event['path']['asset_id'] asset = AssetModel.get(hash_key=asset_id) asset.mark_uploaded() except AssertionError as e: return { 'statusCode': httplib.PRECONDITION_FAILED, 'body': { 'error_message': 'ASSET {} state incorrect: {}'.format(asset_id, e) } } except DoesNotExist: return { 'statusCode': httplib.NOT_FOUND, 'body': { 'error_message': 'ASSET {} not found'.format(asset_id) } } return {"statusCode": httplib.ACCEPTED, "body": {'status': asset.state}}
def update(event, context): logger.debug('event: {}'.format(event)) try: asset_id = event['path']['asset_id'] asset = AssetModel.get(hash_key=asset_id) asset.mark_uploaded() except AssertionError as e: return { 'statusCode': httplib.PRECONDITION_FAILED, 'body': { 'error_message': 'ASSET {} state incorrect: {}'.format(asset_id, e) } } except DoesNotExist: return { 'statusCode': httplib.NOT_FOUND, 'body': { 'error_message': 'ASSET {} not found'.format(asset_id) } } return { "statusCode": httplib.ACCEPTED, "body": { 'status': asset.state } }
def event(event, context): """ Triggered by s3 events, object create and remove """ logger.debug('event: {}'.format(event)) event_name = event['Records'][0]['eventName'] key = event['Records'][0]['s3']['object']['key'] asset_id = key.replace('{}/'.format(os.environ['S3_KEY_BASE']), '') try: if 'ObjectCreated:Put' == event_name: try: asset = AssetModel.get(hash_key=asset_id) asset.mark_received() except UpdateError: return { 'statusCode': httplib.BAD_REQUEST, 'body': { 'error_message': 'Unable to update ASSET'} } elif 'ObjectRemoved:Delete' == event_name: try: asset = AssetModel.get(hash_key=asset_id) asset.delete() except DeleteError: return { 'statusCode': httplib.BAD_REQUEST, 'body': { 'error_message': 'Unable to delete ASSET {}'.format(asset) } } except DoesNotExist: return { 'statusCode': httplib.NOT_FOUND, 'body': { 'error_message': 'ASSET {} not found'.format(asset_id) } } return {'statusCode': httplib.ACCEPTED}
def delete(event, context): logger.debug('event: {}'.format(event)) try: asset_id = event['path']['asset_id'] asset = AssetModel.get(hash_key=asset_id) except DoesNotExist: return { 'statusCode': httplib.NOT_FOUND, 'body': { 'error_message': 'ASSET {} not found'.format(asset_id) } } try: asset.mark_deleted() except DeleteError: return { 'statusCode': httplib.BAD_REQUEST, 'body': { 'error_message': 'Unable to delete ASSET {}'.format(asset) } } return {'statusCode': httplib.NO_CONTENT}
def get(event, context): """ Get a presigned download URL for asset <asset-id> """ logger.debug('event: {}'.format(event)) try: ttl = os.environ['URL_DEFAULT_TTL'] try: ttl = int(event['query']['timeout']) except KeyError or ValueError: pass asset_id = event['path']['asset_id'] asset = AssetModel.get(hash_key=asset_id) download_url = asset.get_download_url(ttl) except DoesNotExist: return { 'statusCode': httplib.NOT_FOUND, 'body': { 'error_message': 'ASSET {} not found'.format(asset_id) } } except AssertionError as e: return { 'statusCode': httplib.FORBIDDEN, 'body': { 'error_message': 'Unable to download: {}'.format(e) } } return { "statusCode": httplib.ACCEPTED, "body": { 'download_url': download_url } }
def get(event, context): """ Get a presigned download URL for asset <asset-id> """ # Sample events using different lambda integrations: # # _lambda_event = { # 'body': {}, 'method': 'GET', 'principalId': '', 'stage': 'dev', 'cognitoPoolClaims': {'sub': ''}, # 'headers': {'Accept': '*/*', 'CloudFront-Forwarded-Proto': 'https', 'CloudFront-Is-Desktop-Viewer': 'true', # 'CloudFront-Is-Mobile-Viewer': 'false', 'CloudFront-Is-SmartTV-Viewer': 'false', # 'CloudFront-Is-Tablet-Viewer': 'false', 'CloudFront-Viewer-Country': 'US', # 'Host': 'c1xblyjsid.execute-api.us-east-1.amazonaws.com', 'User-Agent': 'curl/7.56.1', # 'Via': '1.1 57933097ddb189ecc8b3745fb94cfa94.cloudfront.net (CloudFront)', # 'X-Amz-Cf-Id': 'W95mJn3pc3G8T85Abt2Dj_wLPE_Ar_q0k56uF5yreiaNOMn6P2Nltw==', # 'X-Amzn-Trace-Id': 'Root=1-5a1b453d-1e857d3548e38a1c2827969e', # 'X-Forwarded-For': '75.82.111.45, 216.137.44.17', 'X-Forwarded-Port': '443', # 'X-Forwarded-Proto': 'https'}, 'query': {}, # 'path': {'asset_id': '0e4e06c6-d2fc-11e7-86c6-6672893a702e'}, # 'identity': {'cognitoIdentityPoolId': '', 'accountId': '', 'cognitoIdentityId': '', 'caller': '', # 'apiKey': '', 'sourceIp': '75.82.111.45', 'accessKey': '', 'cognitoAuthenticationType': '', # 'cognitoAuthenticationProvider': '', 'userArn': '', 'userAgent': 'curl/7.56.1', 'user': ''}, # 'stageVariables': {}} # # _lambda_event_with_timeout = { # 'body': {}, 'method': 'GET', 'principalId': '', 'stage': 'dev', # 'cognitoPoolClaims': {'sub': ''}, # 'headers': {'Accept': '*/*', 'CloudFront-Forwarded-Proto': 'https', # 'CloudFront-Is-Desktop-Viewer': 'true', # 'CloudFront-Is-Mobile-Viewer': 'false', # 'CloudFront-Is-SmartTV-Viewer': 'false', # 'CloudFront-Is-Tablet-Viewer': 'false', 'CloudFront-Viewer-Country': 'US', # 'Host': 'c1xblyjsid.execute-api.us-east-1.amazonaws.com', # 'User-Agent': 'curl/7.56.1', # 'Via': '1.1 7acf1813f9ec06038d676de15fcfc28f.cloudfront.net (CloudFront)', # 'X-Amz-Cf-Id': 'RBFBVYMys7aDqQ8u2Ktqvd-ZNwy-Kg7LPZ9LBTe-42nnx1wh0b5bGg==', # 'X-Amzn-Trace-Id': 'Root=1-5a1b4655-785e402d33e13e9d533281ef', # 'X-Forwarded-For': '75.82.111.45, 216.137.44.103', # 'X-Forwarded-Port': '443', 'X-Forwarded-Proto': 'https'}, # 'query': {'timeout': '1000000'}, # 'path': {'asset_id': '0e4e06c6-d2fc-11e7-86c6-6672893a702e'}, # 'identity': {'cognitoIdentityPoolId': '', 'accountId': '', 'cognitoIdentityId': '', # 'caller': '', 'apiKey': '', 'sourceIp': '75.82.111.45', 'accessKey': '', # 'cognitoAuthenticationType': '', 'cognitoAuthenticationProvider': '', # 'userArn': '', 'userAgent': 'curl/7.56.1', 'user': ''}, # 'stageVariables': {}} logger.debug('event: {}'.format(event)) try: ttl = os.environ['URL_DEFAULT_TTL'] try: ttl = int(event['query']['timeout']) except KeyError or ValueError: pass asset_id = event['path']['asset_id'] asset = AssetModel.get(hash_key=asset_id) download_url = asset.get_download_url(ttl) except DoesNotExist: return { 'statusCode': httplib.NOT_FOUND, 'body': { 'error_message': 'ASSET {} not found'.format(asset_id) } } except AssertionError as e: return { 'statusCode': httplib.FORBIDDEN, 'body': { 'error_message': 'Unable to download: {}'.format(e) } } return { "statusCode": httplib.ACCEPTED, "body": { 'download_url': download_url } }