【问题标题】:Lambda with S3 trigger returning NoSuchKey error (Python)带有 S3 触发器的 Lambda 返回 NoSuchKey 错误(Python)
【发布时间】:2021-12-16 03:58:34
【问题描述】:

我按照本教程设置了在文件上传到我的 S3 存储桶时触发的 Lambda:https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html

我还设置了一个前缀(本教程没有),以便只定位一个文件夹:前缀:posters/

我的 S3 存储桶中有一个名为 posters/ 的文件夹。反正代码是直接从教程里抄来的:

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

并且测试事件已经配置如下:

{
  "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"
        }
      }
    }
  ]
}

当我对此进行测试时,我收到以下错误:

{
  "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"
  ]
}

我到底做错了什么?显然权限很好,因为在为函数角色设置正确的策略后,该问题就消失了。非常感谢任何帮助。

【问题讨论】:

  • 您能否检查您的 s3 存储桶,下次尝试时,您能否为键和存储桶变量添加 print 语句以进行调试?

标签: python amazon-web-services amazon-s3 aws-lambda


【解决方案1】:

NoSuchKey 表示文件不在 bucket+key 显示的位置。

您是否在存储桶的posters 文件夹中上传了一个名为HappyFace.jpg 的测试文件(按照教程Create a bucket and upload a sample object)?

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2018-01-26
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 2017-08-12
    • 1970-01-01
    相关资源
    最近更新 更多