gpt4 book ai didi

python - AWS Lambda 中缺少处理程序错误

转载 作者:太空狗 更新时间:2023-10-30 00:42:44 33 4
gpt4 key购买 nike

对于基本问题,我深表歉意。我是 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'"

最佳答案

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

关于python - AWS Lambda 中缺少处理程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48266106/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com