Example #1
0
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}
Example #2
0
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}}
Example #3
0
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
        }
    }
Example #4
0
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}
Example #5
0
def asset_list(event, context):
    logger.debug('event: {}, context: {}'.format(event, context))

    results = AssetModel.scan()
    return {
        'statusCode': httplib.OK,
        'body': {
            'items': [dict(result) for result in results]
        }
    }
Example #6
0
def asset_list(event, context):
    logger.debug('event: {}, context: {}'.format(event, context))

    results = AssetModel.scan()
    return {
        'statusCode': httplib.OK,
        'body': {
            'items': [dict(result) for result in results]
        }
    }
Example #7
0
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}
Example #8
0
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
        }
    }
Example #9
0
def create(event, context):
    """
     No body needed here as POST is a request for a pre-signed upload URL.
     Create an entry for it in dynamo and return upload URL
    """
    logger.debug('event: {}'.format(event))
    asset = AssetModel()
    asset.asset_id = uuid.uuid1().__str__()
    asset.save()
    upload_url = asset.get_upload_url(
    )  # No timeout specified here, use member param default

    return {
        "statusCode": httplib.CREATED,
        "body": {
            'upload_url': upload_url,
            'id': asset.asset_id
        }
    }
Example #10
0
def create(event, context):
    """
     No body needed here as POST is a request for a pre-signed upload URL.
     Create an entry for it in dynamo and return upload URL
    """
    # Sample events using different lambda integrations:
    #
    # _lambda_proxy_event = {'resource': '/asset', 'path': '/asset', 'httpMethod': 'POST',
    #                        '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 5c75b37c7e0aa5868b6499a5c4448d1f.cloudfront.net (CloudFront)',
    #                                    'X-Amz-Cf-Id': 'XG5WkkaGYGdbA9KAm7Hsbl5t7D7KmALE4Q2LdOwbXYoCFJZxyyiARw==',
    #                                    'X-Amzn-Trace-Id': 'Root=1-5a1b28a6-2b6e5ef6657e0f5f2d671017',
    #                                    'X-Forwarded-For': '75.82.111.45, 216.137.44.44', 'X-Forwarded-Port': '443',
    #                                    'X-Forwarded-Proto': 'https'}, 'queryStringParameters': None,
    #                        'pathParameters': None, 'stageVariables': None,
    #                        'requestContext': {'requestTime': '26/Nov/2017:20:48:38 +0000', 'path': '/dev/asset',
    #                                           'accountId': '818300131735', 'protocol': 'HTTP/1.1',
    #                                           'resourceId': 'wpjmgf', 'stage': 'dev', 'requestTimeEpoch': 1511729318077,
    #                                           'requestId': '2d827060-d2eb-11e7-96f5-9b58ecc94e3f',
    #                                           'identity': {'cognitoIdentityPoolId': None, 'accountId': None,
    #                                                        'cognitoIdentityId': None, 'caller': None, 'apiKey': '',
    #                                                        'sourceIp': '75.82.111.45', 'accessKey': None,
    #                                                        'cognitoAuthenticationType': None,
    #                                                        'cognitoAuthenticationProvider': None, 'userArn': None,
    #                                                        'userAgent': 'curl/7.56.1', 'user': None},
    #                                           'resourcePath': '/asset', 'httpMethod': 'POST', 'apiId': 'c1xblyjsid'},
    #                        'body': None, 'isBase64Encoded': False}
    #
    # _lambda_event = {'body': {}, 'method': 'POST', '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 022c901b294fedd7074704d46fce9819.cloudfront.net (CloudFront)',
    #                              'X-Amz-Cf-Id': 'BifKUMLw8qO30TNbJ4QObNGq6WVxiL9nTv9eMbRtAIqqHIqQDkZEVw==',
    #                              'X-Amzn-Trace-Id': 'Root=1-5a1b387c-47ab478111bbb2eb6bd6530c',
    #                              'X-Forwarded-For': '75.82.111.45, 216.137.44.14', 'X-Forwarded-Port': '443',
    #                              'X-Forwarded-Proto': 'https'}, 'query': {}, 'path': {},
    #                  '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))
    asset = AssetModel()
    asset.asset_id = uuid.uuid1().__str__()
    asset.save()
    upload_url = asset.get_upload_url()  # No timeout specified here, use member param default

    return {
        "statusCode": httplib.CREATED,
        "body": {
            'upload_url': upload_url,
            'id': asset.asset_id
        }
    }
Example #11
0
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
        }
    }
Example #12
0
def create(event, context):
    """
     No body needed here as POST is a request for a pre-signed upload URL.
     Create an entry for it in dynamo and return upload URL
    """
    # Sample events using different lambda integrations:
    #
    # _lambda_proxy_event = {'resource': '/asset', 'path': '/asset', 'httpMethod': 'POST',
    #                        '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 5c75b37c7e0aa5868b6499a5c4448d1f.cloudfront.net (CloudFront)',
    #                                    'X-Amz-Cf-Id': 'XG5WkkaGYGdbA9KAm7Hsbl5t7D7KmALE4Q2LdOwbXYoCFJZxyyiARw==',
    #                                    'X-Amzn-Trace-Id': 'Root=1-5a1b28a6-2b6e5ef6657e0f5f2d671017',
    #                                    'X-Forwarded-For': '75.82.111.45, 216.137.44.44', 'X-Forwarded-Port': '443',
    #                                    'X-Forwarded-Proto': 'https'}, 'queryStringParameters': None,
    #                        'pathParameters': None, 'stageVariables': None,
    #                        'requestContext': {'requestTime': '26/Nov/2017:20:48:38 +0000', 'path': '/dev/asset',
    #                                           'accountId': '818300131735', 'protocol': 'HTTP/1.1',
    #                                           'resourceId': 'wpjmgf', 'stage': 'dev', 'requestTimeEpoch': 1511729318077,
    #                                           'requestId': '2d827060-d2eb-11e7-96f5-9b58ecc94e3f',
    #                                           'identity': {'cognitoIdentityPoolId': None, 'accountId': None,
    #                                                        'cognitoIdentityId': None, 'caller': None, 'apiKey': '',
    #                                                        'sourceIp': '75.82.111.45', 'accessKey': None,
    #                                                        'cognitoAuthenticationType': None,
    #                                                        'cognitoAuthenticationProvider': None, 'userArn': None,
    #                                                        'userAgent': 'curl/7.56.1', 'user': None},
    #                                           'resourcePath': '/asset', 'httpMethod': 'POST', 'apiId': 'c1xblyjsid'},
    #                        'body': None, 'isBase64Encoded': False}
    #
    # _lambda_event = {'body': {}, 'method': 'POST', '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 022c901b294fedd7074704d46fce9819.cloudfront.net (CloudFront)',
    #                              'X-Amz-Cf-Id': 'BifKUMLw8qO30TNbJ4QObNGq6WVxiL9nTv9eMbRtAIqqHIqQDkZEVw==',
    #                              'X-Amzn-Trace-Id': 'Root=1-5a1b387c-47ab478111bbb2eb6bd6530c',
    #                              'X-Forwarded-For': '75.82.111.45, 216.137.44.14', 'X-Forwarded-Port': '443',
    #                              'X-Forwarded-Proto': 'https'}, 'query': {}, 'path': {},
    #                  '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))
    asset = AssetModel()
    asset.asset_id = uuid.uuid1().__str__()
    asset.save()
    upload_url = asset.get_upload_url(
    )  # No timeout specified here, use member param default

    return {
        "statusCode": httplib.CREATED,
        "body": {
            'upload_url': upload_url,
            'id': asset.asset_id
        }
    }