AWS Lambda函数引发“导入模块错误”

问题描述

按照本教程操作:https://levelup.gitconnected.com/resize-an-image-in-aws-s3-using-lambda-function-dc386afd4128

有问题的代码用于在上传到存储分区x时调整图片大小,并将调整后的图片放入存储分区x调整大小中。

一切正常,直到我进入测试阶段,他的s3 zip文件被接受,但是测试阶段引发以下错误:

{
  "errorMessage": "Unable to import module 'CreateThumbnail': No module named 'CreateThumbnail'","errorType": "Runtime.ImportModuleError"
}

我还应该提到我将他的CreateThumbnail脚本从

def handler(event,context):
    for record in event['Records']:
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key'] 
        download_path = '/tmp/{}{}'.format(uuid.uuid4(),key)
        upload_path = '/tmp/resized-{}'.format(key)

def handler(event,context):
    for record in event['Records']:
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key'] 
        download_path = '/{}{}'.format(uuid.uuid4(),key)
        upload_path = '/resized-{}'.format(key)

根据我的想法,脚本从文件夹“ tmp”“下载”原始图像,并将其上传到我的两个存储桶中都没有的文件夹“ tmp”,因为我希望在根。

即使zip文件包含所有必需的库,如果有人能告诉我为什么会发生这种情况,那就太好了(和CreateThumbnail.py)。谢谢。

解决了导入模块的问题,现在是这个问题:

{
  "errorMessage": "Failed to upload /tmp/resized-HappyFace.jpg to flatoro-resized/HappyFace.jpg: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied","errorType": "S3UploadFailedError","stackTrace": [
    "  File \"/var/task/CreateThumbnail.py\",line 24,in handler\n    s3_client.upload_file(upload_path,'{}-resized'.format(bucket),key)\n","  File \"/var/task/boto3/s3/inject.py\",line 131,in upload_file\n    extra_args=ExtraArgs,callback=Callback)\n","  File \"/var/task/boto3/s3/transfer.py\",line 287,in upload_file\n    filename,'/'.join([bucket,key]),e))\n"
  ]
}

解决方法

不看代码很难说,但是在错误消息中我可以看到“ s3_client.upload_file(upload_path,'{} -resize'.format(bucket),key)”。在此处的source_location(需要从中复制文件的位置)中,而不是在download_path(lambda的/ tmp /文件名)中写入upload_path。

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.upload_file

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...