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.
record['s3']['object']['key']? You can debug viaprint(record)and see what it contains.objectKey = urllib.parse.unquote_plus(record['s3']['object']['key']))