0

I have an SQS queue which triggers my Lambda function. I am trying to parse the SQS message and extract relevant data. I am specifically trying to extract the s3 object key and the s3 bucket arn.

My current Lambda function:

def lambda_handler(event, context):
       
    for record in event['Records']:
        payload = record['body']
        print(str(payload))

        objectKey = record['body']['s3']['object']['key']
        bucketARN = record['body']['s3']['bucket']['arn']
        
        print('Filename is: ' + str(objectKey))
        print('Bucket ARN is: ' + str(bucketARN))

My SQS message that is shown in CloudWatch:

{
    "Records": [
        {
            "eventVersion": "2.1",
            "eventSource": "aws:s3",
            "awsRegion": "xxx",
            "eventTime": "2021-03-27T19:39:41.694Z",
            "eventName": "ObjectCreated:Put",
            "userIdentity": {
                "principalId": "AWS:AIDAQ5MMP4GQL2S4KQ2SZ"
            },
            "requestParameters": {
                "sourceIPAddress": "xxx"
            },
            "responseElements": {
                "x-amz-request-id": "xxx",
                "x-amz-id-2": "xxx"
            },
            "s3": {
                "s3SchemaVersion": "1.0",
                "configurationId": "xxx",
                "bucket": {
                    "name": "xxx",
                    "ownerIdentity": {
                        "principalId": "AHWVPEVW911DW"
                    },
                    "arn": "arn:aws:s3:::xxx"
                },
                "object": {
                    "key": "Audio1.mp3",
                    "size": 16659,
                    "eTag": "71d5ea0a7152e04d0d170be8146080ba",
                    "sequencer": "00605F8A02147F848A"
                }
            }
        }
    ]
}

And lastly the error I'm being given:

[ERROR] TypeError: string indices must be integers
Traceback (most recent call last):
  File "/var/task/transcribe.py", line 14, in lambda_handler
    objectKey = record['body']['s3']['object']['key']

I have also tried objectKey = record['Records][0]['s3']['object']['key']. Thanks.

2
  • Wouldn't it just be record['s3']['object']['key']? You can debug via print(record) and see what it contains. Commented Mar 28, 2021 at 2:43
  • You might want to use: objectKey = urllib.parse.unquote_plus(record['s3']['object']['key'])) Commented Mar 28, 2021 at 2:45

1 Answer 1

0

Found a helpful answer to a similar question here.

This worked for me:

for record in event['Records']:     
        #pull the body out & json load it
        jsonmaybe = record['body']
        jsonmaybe = json.loads(jsonmaybe)
        
        objectKey = jsonmaybe['Records'][0]['s3']['object']['key']
        print(objectKey)
        bucketARN = jsonmaybe['Records'][0]['s3']['bucket']['arn']
        print(bucketARN)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.