AWS Lambda中缺少处理程序错误

chp*_*sam 5 python amazon-web-services aws-lambda

我对基本问题表示歉意。我对AWS和Python都是全新的。我正在尝试执行https://boto3.readthedocs.io/en/latest/guide/migrations3.html#accessing-a-bucket中给出的示例代码,但遇到错误。

import botocore
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucketname')
exists = True


try:
    s3.meta.client.head_bucket(Bucket='bucketname')
except botocore.exceptions.ClientError as e:
    # If a client error is thrown, then check that it was a 404 error.
    # If it was a 404 error, then the bucket does not exist.
    error_code = int(e.response['Error']['Code'])
    if error_code == 404:
        exists = False 
Run Code Online (Sandbox Code Playgroud)

日志中的错误是

“ errorMessage”:“模块'lambda_function'上缺少处理程序'lambda_handler'”

kri*_*004 12

您需要在代码中定义一个函数。该代码缺少名为的函数lambda_handler。您的代码应如下所示:

    import botocore
    import boto3

    def lambda_handler(event, context):
        s3 = boto3.resource('s3')
        bucket = s3.Bucket('bucketname')
        exists = True

        try:
            s3.meta.client.head_bucket(Bucket='bucketname')
        except botocore.exceptions.ClientError as e:
            # If a client error is thrown, then check that it was a 404 error.
            # If it was a 404 error, then the bucket does not exist.
            error_code = int(e.response['Error']['Code'])
            if error_code == 404:
                exists = False
Run Code Online (Sandbox Code Playgroud)


aji*_*633 6

将代码移至 python 函数内。您可以给它任何名称,但这将成为您的处理程序。转到 lambda 函数基本设置并将默认处理程序更改为<yourfilename>_<yourfunctionname>。默认情况下,当您创建 lambda 函数时,文件名将为lambda_function_name.py(您可以更改它),您的处理程序方法将为lambda_handler(您可以更改它),因此入口点是lambda_function_name.lambda_handler.