未定义模块初始化错误名称“事件”

时间:2020-03-29 14:26:54

标签: python amazon-s3 aws-lambda

我正在尝试通过AWS Lamda Function将文件从一个存储桶复制到另一个存储桶。我的AWS S3触发器设置 enter image description here 这是代码--->

from __future__ import print_function

    import boto3
    import json
    import time
    import urllib

    print("loadig function")
    s3 = boto3.client('s3')

    def lambda_handler(event, context):
        source_bucket = event['Records'][0]['s3']['bucket']['name']


    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'])
    target_bucket = 'destinationstage'
    copy_source = {'Bucket': source_bucket, 'Key': key}
    try:
        print("waiting for the file persist in the source bucket")
        waiter = s3.get_waiter('object_exists')
        waiter.wait(Bucket=source_bucket, key=key)
        print(
            "copying the object from the source s3 bucket to destination s3 bucket"
        )
        s3.copy_object(Bucket=target_bucket, Key=key, CopySource=copy_source)

    except Exception as e:
        print(e)
        print(
            "Error getting object {} from bucket {}. Make sure tehy exists in the bucket"
        )
        raise e

这是我一直收到的错误消息--->

Request ID:
"628ad4ad-3355-492f-8daf-019a6b9c655c"

Function Logs:
START RequestId: 628ad4ad-3355-492f-8daf-019a6b9c655c Version: $LATEST
module initialization error: name 'event' is not defined

END RequestId: 628ad4ad-3355-492f-8daf-019a6b9c655c
REPORT RequestId: 628ad4ad-3355-492f-8daf-019a6b9c655c  Duration: 598.71 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 67 MB  Init Duration: 343.97 ms    
module initialization error
name 'event' is not defined

1 个答案:

答案 0 :(得分:0)

这只是一个缩进问题。更改为:

import boto3
import json
import time
import urllib

print("loadig function")
s3 = boto3.client('s3')

def lambda_handler(event, context):
    source_bucket = event['Records'][0]['s3']['bucket']['name']


    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'])
    ...
相关问题