Alw*_*nny 1 python amazon-web-services python-imaging-library boto3 aws-lambda
我正在尝试将源图像的大小调整为多个维度 + 扩展名。
例如:当我上传源图像时,比如说 abc.jpg,我需要使用 s3 事件触发器将其大小调整为 .jpg 和 .webp 的大小,例如abc_320.jpg、abc_320.webp、abc_640.jpg、abc_640.webp。因此,使用我当前的 python lambda 处理程序,我可以通过多次put_object调用目标存储桶来完成它,但我想让它更加优化,因为将来我的维度+扩展可能会增加。那么如何通过一次调用将所有调整大小的图像存储到目标存储桶?
当前的 Lambda 处理程序:
import json
import boto3
import os
from os import path
from io import BytesIO
from PIL import Image
# boto3 S3 initialization
s3_client = boto3.client("s3")
def lambda_handler(event, context):
destination_bucket_name = 'destination-bucket'
# event contains all information about uploaded object
print("Event :", event)
# Bucket Name where file was uploaded
source_bucket_name = event['Records'][0]['s3']['bucket']['name']
# Filename of object (with path)
dest_bucket_perfix = 'resized'
file_key_name = event['Records'][0]['s3']['object']['key']
image_obj = s3_client.get_object(Bucket=source_bucket_name, Key=file_key_name)
image_obj = image_obj.get('Body').read()
img = Image.open(BytesIO(image_obj))
dimensions = [320, 640]
# Checking the extension and
img_extension = path.splitext(file_key_name)[1].lower()
extension_dict = {".jpg":"JPEG", ".png":"PNG", ".jpeg":"JPEG"}
extensions = ["WebP"]
if img_extension in extension_dict.keys():
extensions.append(extension_dict[img_extension])
print ("test-1")
for dimension in dimensions:
WIDTH = HEIGHT = dimension
for extension in extensions:
resized_img = img.resize((WIDTH, HEIGHT))
buffer = BytesIO()
resized_img.save(buffer, extension)
buffer.seek(0)
# I don't want to use this put_object in loop <<<---
s3_client.put_object(Bucket=destination_bucket_name, Key=file_key_name.replace("upload", dest_bucket_perfix, 1), Body=buffer)
return {
'statusCode': 200,
'body': json.dumps('Hello from S3 events Lambda!')
}
Run Code Online (Sandbox Code Playgroud)
你可以看到我需要调用put_object维度+扩展的每次迭代,这是代价高昂的。我也考虑过多线程和压缩解决方案,但正在寻找其他可能的想法/解决方案
| 归档时间: |
|
| 查看次数: |
29 次 |
| 最近记录: |