I followed this tutorial to set up my Lambda that gets triggered when a file is uploaded to my S3 bucket: https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html
I also set a prefix (which the tutorial doesn't have) so that only one folder is targeted: Prefix: posters/
I have a folder in my S3 bucket called posters/
. Anyway, the code is directly copied from the tutorial:
import json
import urllib.parse
import boto3
print('Loading function')
s3 = boto3.client('s3', 'us-east-2')
def lambda_handler(event, context):
#print("Received event: " json.dumps(event, indent=2))
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
try:
response = s3.get_object(Bucket=bucket, Key=key)
print("CONTENT TYPE: " response['ContentType'])
return response['ContentType']
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e
And the test event has been configured as follows:
{
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "us-east-2",
"eventTime": "1970-01-01T00:00:00.000Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"responseElements": {
"x-amz-request-id": "EXAMPLE123456789",
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {
"name": "MY_BUCKET_NAME",
"ownerIdentity": {
"principalId": "EXAMPLE"
},
"arn": "arn:aws:s3:::example-bucket"
},
"object": {
"key": "posters/HappyFace.jpg",
"size": 1024,
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901"
}
}
}
]
}
When I test this, I get the following error:
{
"errorMessage": "An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.",
"errorType": "NoSuchKey",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 23, in lambda_handler\n raise e\n",
" File \"/var/task/lambda_function.py\", line 17, in lambda_handler\n response = s3.get_object(Bucket=bucket, Key=key)\n",
" File \"/var/runtime/botocore/client.py\", line 386, in _api_call\n return self._make_api_call(operation_name, kwargs)\n",
" File \"/var/runtime/botocore/client.py\", line 705, in _make_api_call\n raise error_class(parsed_response, operation_name)\n"
]
}
What exactly am I doing wrong here? Obviously the permissions are fine, since that issue went away after setting the right policy to the function role. Any help is greatly appreciated.
CodePudding user response:
NoSuchKey
means the file isn't where the bucket key shows it should be.
Did you upload a test file named HappyFace.jpg
inside the posters
folder in your bucket (as per the tutorial Create a bucket and upload a sample object
)?