我使用Lambda-uploader來編寫python lambda代碼並將zip移動到AWS。 我創建了一個包含我的jar文件和zip文件夾結構,如下所示。Lambda-Uploader:無法導入模塊'CreateThumbnail':無法導入名稱_imaging
我正在使用的代碼是從AWS門戶和使用PIL類。我在Lambda-uploader中包含了Pillow庫,但是當我通過導入創建的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)
檢查:http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html 我不知道python太多,但在java如果我需要外部庫,那麼我們需要創建包含所有外部庫的胖jar/zip文件。 – Suyash
謝謝。我在這裏也是這樣。我在我的問題中包含了zip文件結構。 – sina
我在下面的鏈接中發現了類似的問題: https://stackoverflow.com/questions/25340698/importerror-cannot-import-name-imaging – Suyash