【发布时间】: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