简体   繁体   English

带有 S3 触发器的 Lambda 返回 NoSuchKey 错误(Python)

[英]Lambda with S3 trigger returning NoSuchKey error (Python)

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我按照本教程设置了当文件上传到我的 S3 存储桶时触发的 Lambda: 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/ .我的 S3 存储桶中有一个名为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.任何帮助是极大的赞赏。

NoSuchKey means the file isn't where the bucket+key shows it should be. NoSuchKey意味着文件不在存储桶+密钥显示的位置。

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 )?您是否在存储桶的posters文件夹中上传了名为HappyFace.jpg的测试文件(根据教程Create a bucket and upload a sample object )?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM