Ejemplo n.º 1
0
def test_s3_trigger_event():
    event = S3Event(load_event("s3Event.json"))
    records = list(event.records)
    assert len(records) == 1
    record = records[0]
    assert record.event_version == "2.1"
    assert record.event_source == "aws:s3"
    assert record.aws_region == "us-east-2"
    assert record.event_time == "2019-09-03T19:37:27.192Z"
    assert record.event_name == "ObjectCreated:Put"
    user_identity = record.user_identity
    assert user_identity.principal_id == "AWS:AIDAINPONIXQXHT3IKHL2"
    request_parameters = record.request_parameters
    assert request_parameters.source_ip_address == "205.255.255.255"
    assert record.response_elements["x-amz-request-id"] == "D82B88E5F771F645"
    s3 = record.s3
    assert s3.s3_schema_version == "1.0"
    assert s3.configuration_id == "828aa6fc-f7b5-4305-8584-487c791949c1"
    bucket = s3.bucket
    assert bucket.name == "lambda-artifacts-deafc19498e3f2df"
    assert bucket.owner_identity.principal_id == "A3I5XTEXAMAI3E"
    assert bucket.arn == "arn:aws:s3:::lambda-artifacts-deafc19498e3f2df"
    assert s3.get_object.key == "b21b84d653bb07b05b1e6b33684dc11b"
    assert s3.get_object.size == 1305107
    assert s3.get_object.etag == "b21b84d653bb07b05b1e6b33684dc11b"
    assert s3.get_object.version_id is None
    assert s3.get_object.sequencer == "0C0F6F405D6ED209E1"
    assert record.glacier_event_data is None
    assert event.record.raw_event == event["Records"][0]
    assert event.bucket_name == "lambda-artifacts-deafc19498e3f2df"
    assert event.object_key == "b21b84d653bb07b05b1e6b33684dc11b"
Ejemplo n.º 2
0
def generic_sns_s3_delete_event(base_sns_event,
                                generic_s3_delete_notification):
    '''Return an s3 put event'''
    s3_notification_event = S3Event(generic_s3_delete_notification)
    base_sns_event['Records'][0]['Sns']['Message'] = json.dumps(
        s3_notification_event._data)
    return base_sns_event
Ejemplo n.º 3
0
def test_handler(event, s3_notification, context, mocker):
    '''Call handler'''

    s3_notification_event = S3Event(s3_notification)
    event['Records'][0]['Sns']['Message'] = json.dumps(
        s3_notification_event._data)

    resp = func.handler(event, context)

    assert resp == s3_notification
Ejemplo n.º 4
0
def s3_put_event(generic_s3_put_notification, sns_topic_arn,
                 request) -> S3Event:
    '''Return an event referencing a real S3 object'''
    generic_s3_put_notification['Records'][0]["s3"]["bucket"][
        "name"] = BUCKET_NAME
    generic_s3_put_notification['Records'][0]["s3"]["bucket"][
        "arn"] = 'arn:aws:s3:::{}'.format(BUCKET_NAME)
    generic_s3_put_notification['Records'][0]["s3"]["object"][
        "key"] = '{}/{}'.format(BUCKET_PATH, request.param)
    s3_notification_event = S3Event(generic_s3_put_notification)
    return s3_notification_event
Ejemplo n.º 5
0
def handler(event: Dict[str, Any], context: LambdaContext) -> dict:
    '''Function entry'''

    LOGGER.info('Event', extra={"message_object": event})
    sns_event = SNSEvent(event)
    sns_message_str = json.loads(sns_event.sns_message)
    s3_event = S3Event(sns_message_str)
    s3_event_data = s3_event._data

    LOGGER.info('Response', extra={"message_object": s3_event_data})
    return s3_event_data
Ejemplo n.º 6
0
def test_s3_key_unquote_plus():
    tricky_name = "foo name+value"
    event_dict = {
        "Records": [{
            "s3": {
                "object": {
                    "key": quote_plus(tricky_name)
                }
            }
        }]
    }
    event = S3Event(event_dict)
    assert event.object_key == tricky_name
Ejemplo n.º 7
0
def test_s3_glacier_event():
    example_event = {
        "Records": [{
            "glacierEventData": {
                "restoreEventData": {
                    "lifecycleRestorationExpiryTime":
                    "1970-01-01T00:01:00.000Z",
                    "lifecycleRestoreStorageClass": "standard",
                }
            }
        }]
    }
    event = S3Event(example_event)
    record = next(event.records)
    glacier_event_data = record.glacier_event_data
    assert glacier_event_data is not None
    assert glacier_event_data.restore_event_data.lifecycle_restoration_expiry_time == "1970-01-01T00:01:00.000Z"
    assert glacier_event_data.restore_event_data.lifecycle_restore_storage_class == "standard"
Ejemplo n.º 8
0
def handler(event: Dict[str, Any], context: LambdaContext) -> Response:
    '''Function entry'''
    LOGGER.info('Event', extra={"message_object": event})

    s3_event = S3Event(event)

    s3_bucket = s3_event.bucket_name
    s3_object = s3_event.object_key
    object_size = s3_event.record.s3.get_object.size
    pk = '{}#{}'.format(s3_bucket, s3_object)
    sk = 'exif#v0'

    (exif_data, file_data) = _get_exif_data(s3_bucket, s3_object, object_size)
    exif_data_item = ExifDataItem(**{
        'pk': pk,
        'sk': sk,
        'file': file_data,
        'exif': exif_data
    })

    response = Response(**{'Item': exif_data_item})
    LOGGER.info('Response', extra={"message_object": asdict(response)})
    return response
Ejemplo n.º 9
0
def test_s3_glacier_event_json():
    event = S3Event(load_event("s3EventGlacier.json"))
    glacier_event_data = event.record.glacier_event_data
    assert glacier_event_data is not None
    assert glacier_event_data.restore_event_data.lifecycle_restoration_expiry_time == "1970-01-01T00:01:00.000Z"
    assert glacier_event_data.restore_event_data.lifecycle_restore_storage_class == "standard"
Ejemplo n.º 10
0
def test_s3_key_url_decoded_key():
    event = S3Event(load_event("s3EventDecodedKey.json"))
    assert event.object_key == event.record["s3"]["object"]["urlDecodedKey"]