【问题标题】:AWS Lambda function throws "Import module error"AWS Lambda 函数抛出“导入模块错误”
【发布时间】:2020-09-04 02:12:55
【问题描述】:

跟随本教程:https://levelup.gitconnected.com/resize-an-image-in-aws-s3-using-lambda-function-dc386afd4128

有问题的代码用于在上传到存储桶 x 并将调整大小的图像放入存储桶 x-resized 时调整图像大小。

在我进入测试阶段之前一切正常,他的 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"
  ]
}

【问题讨论】:

  • 请注意,您只能写入 lambda 中的 /tmp 位置。
  • 你还没有发布导入 CreateThumbnail.py 的代码
  • 您是否已将函数中的处理程序指定为CreateThumbnail.handler? zip 似乎有效(未使用 S3 测试,但通过了您的错误)。我刚刚用过。
  • 是的,我有。我知道代码本身可以工作,它肯定与 AWS 有关。你是什​​么意思?我真的需要创建一个 tmp 文件夹吗?我的存储桶需要一些特殊设置吗?
  • @dantevn5 你能粘贴你的完整代码吗,你的 lambda 处理程序的名称是什么。正如其他人说代码正在运行一样,我怀疑您在代码/配置中缺少某些东西。

标签: python amazon-web-services amazon-s3 aws-lambda image-resizing


【解决方案1】:

不看代码很难判断,但在错误消息中我可以看到“s3_client.upload_file(upload_path, '{}-resized'.format(bucket), key)”。这里在 source_location( 需要复制文件的地方) 而不是 download_path (lambda's/tmp/filename) upload_path 被写入。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2021-11-20
    • 2019-11-01
    • 1970-01-01
    • 2022-01-06
    • 2021-03-28
    • 1970-01-01
    相关资源
    最近更新 更多