RandomTask
RandomTask

Reputation: 509

Python accessing AWS SQS message created by S3

Whenever a file is uploaded to S3, I appreciate that S3 can write a message to SQS. However, accessing the file name ("key":"filename.txt") in the SQS message body from Python is a bit problematic since it's a dictionary that contains a list with multiple dictionaries.

Has anyone accessed the filename in the SQS message body when that message was created by an S3 event?

The full message is:

{
    "Records": [{
        "eventVersion": "2.0",
        "eventSource": "aws:s3",
        "awsRegion": "us-west-2",
        "eventTime": "2016-12-04T22:14:52.325Z",
        "eventName": "ObjectCreated:Put",
        "userIdentity": {
            "principalId": "ABC123"
        },
        "requestParameters": {
            "sourceIPAddress": "12.345.687.899"
        },
        "responseElements": {
            "x-amz-request-id": "ABC123",
            "x-amz-id-2": "ABCDEF"
        },
        "s3": {
            "s3SchemaVersion": "1.0",
            "configurationId": "MyQueueName",
            "bucket": {
                "name": "mybucket",
                "ownerIdentity": {
                    "principalId": "ABC123"
                },
                "arn": "arn:aws:s3:::mybucket"
            },
            "object": {
                "key": "filename.txt",
                "size": 2310,
                "eTag": "defg123",
                "sequencer": "00345"
            }
        }
    }]
}

Upvotes: 3

Views: 5880

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191671

I've fixed your question to be proper JSON.

import json
# message = get_sqs_message()
message = json.loads(message)
print(message["Records"][0]["s3"]["object"]["key"])

Should output filename.txt

Upvotes: 2

Related Questions