【发布时间】:2017-09-07 10:19:41
【问题描述】:
我正在使用 Lambda-uploader 编写 python lambda 代码并将 zip 移动到 AWS。 我创建了一个包含我的 jar 文件和 zip 文件夹结构,如下所示。
我使用的代码来自 AWS 门户,并且正在使用 PIL 类。我将 Pillow 库作为要求包含在 Lambda-uploader 中,但是当我通过导入创建的 zip 文件在 Lambda 控制台上创建我的 Lambda 函数时,我收到以下错误消息。任何帮助表示赞赏。
错误:
START RequestId: e4893543-93aa-11e7-b4b9-89453f1042aa Version: $LATEST
Unable to import module 'CreateThumbnail': cannot import name _imaging
END RequestId: e4893543-93aa-11e7-b4b9-89453f1042aa
REPORT RequestId: e4893543-93aa-11e7-b4b9-89453f1042aa Duration: 0.44 ms Billed Duration: 100 ms Memory Size: 512 MB Max Memory Used: 33 MB
lambda.josn
{
"name": "CreateThumbnail",
"description": "It does things",
"region": "us-east-1",
"runtime": "python2.7",
"handler": "CreateThumbnail.lambda_handler",
"role": "arn:aws:iam::0000000000:role/LambdaTest",
"requirements": ["Pillow"],
"ignore": [
"circle\\.yml$",
"\\.git$",
"/.*\\.pyc$"
],
"timeout": 30,
"memory": 512
}
python 代码:
from __future__ import print_function
import boto3
import os
import sys
import uuid
from PIL import Image
import PIL.Image
s3_client = boto3.client('s3')
def resize_image(image_path, resized_path):
with Image.open(image_path) as image:
image.thumbnail(tuple(x / 2 for x in image.size))
image.save(resized_path)
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)
s3_client.download_file(bucket, key, download_path)
resize_image(download_path, upload_path)
s3_client.upload_file(upload_path, '{}resized'.format(bucket), key)
【问题讨论】:
-
检查:docs.aws.amazon.com/lambda/latest/dg/…我不太了解python,但是在java中如果我需要外部库,那么我们需要创建包含所有外部库的胖jar/zip文件。
-
谢谢。我在这里做了同样的事情。我在我的问题中包含了 zip 文件结构。
-
我在下面的链接中发现了类似的问题:stackoverflow.com/questions/25340698/…
标签: python amazon-web-services lambda python-imaging-library