AWS Lambd中缺少处理程序错误

2024-06-16 10:20:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我为这个基本问题道歉。我对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 

日志中的错误是

"errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'"


Tags: importclientifs3bucket错误existsit
2条回答

Kishnaúu mee2004是对的,您需要定义lambda_handler,否则它永远不会工作,但如果您遇到以下错误:

Handler 'py' missing on module 'jobdata_rdcmedia_s3_Etl_job_scheduler_lambda': 'module' object has no attribute 'py'

然后,您需要检查处理程序信息是否提到lambda_function_name.lambda_handler

您需要在代码中定义一个函数。代码缺少名为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

相关问题 更多 >