【问题标题】:Resize and move Images using lambda function from one s3 to another s3使用 lambda 函数调整图像大小并将图像从一个 s3 移动到另一个 s3
【发布时间】:2020-03-08 13:17:18
【问题描述】:

我想做什么

  • 我有 3 个桶。我想在图像时触发 lambda 函数 上传到bucket1。
  • lambda 函数将调整该图像的大小 (500x500) 并将调整后的图像保存在 bucket2 中。
  • bucket1上的主图会备份到bucket3中。
  • bucket1 上的主镜像将被删除。

到目前为止我做了什么

  • 编写了一个 lambda 函数,可以在存储桶中移动图像。

  • 使 S3 触发 lambda 函数

我的问题在哪里

  • 我正在使用 PIL 来调整图像大小。但是 PIL 模块不在 python STL 中。所以我压缩了我的代码 带有站点包并且能够运行。但是错误说找不到文件。 文件密钥 image/myimage.jpg 的示例

  • 虽然我按照教程进行操作,但如果我尝试使用 lambda 层而不是每次压缩,似乎都找不到 PIL 模块。

代码


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((500, 500))
        image.save(resized_path)

def lambda_handler(event, context):
    #
    # giving a key error here event['Records']
    #
    for record in event['Records']: 
        bucket = 'mahabubelahibucket1'
        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, 'mahabubelahibucket2', key)

【问题讨论】:

  • 您是否忘记在您的 lambda 图层部分添加创建的图层?
  • 嗨@omuthu 不,我没有。首先我创建了一个 lambda 层,然后通过 ARN 在函数中添加该层。
  • 听起来很像:Tutorial: Using AWS Lambda with Amazon S3。本教程包括创建包含所有必需库的 zip 的步骤。

标签: python-3.x amazon-s3 aws-lambda devops


【解决方案1】:

这是我解决问题的方法!

import boto3
import os
from PIL import Image
import pathlib
from io import BytesIO

s3 = boto3.resource('s3')

def delete_this_bucket(name):
    bucket = s3.Bucket(name)
    for key in bucket.objects.all():
        try:
            key.delete()
            bucket.delete()
        except Exception as e:
            print("SOMETHING IS BROKEN !!")

def create_this_bucket(name, location):
    try:
        s3.create_bucket(
            Bucket=name,
            CreateBucketConfiguration={
                'LocationConstraint': location
            }
        )
    except Exception as e:
        print(e)

def upload_test_images(name):
    for each in os.listdir('./testimage'):
        try:
            file = os.path.abspath(each)
            s3.Bucket(name).upload_file(file, each)
        except Exception as e:
            print(e)

def copy_to_other_bucket(src, des, key):
    try:
        copy_source = {
            'Bucket': src,
            'Key': key
        }
        bucket = s3.Bucket(des)
        bucket.copy(copy_source, key)
    except Exception as e:
        print(e)


def resize_image(src_bucket, des_bucket):
    size = 500, 500
    bucket = s3.Bucket(src_bucket)
    in_mem_file = BytesIO()
    client = boto3.client('s3')

    for obj in bucket.objects.all():
        file_byte_string = client.get_object(Bucket=src_bucket, Key=obj.key)['Body'].read()
        im = Image.open(BytesIO(file_byte_string))

        im.thumbnail(size, Image.ANTIALIAS)
        # ISSUE : https://stackoverflow.com/questions/4228530/pil-thumbnail-is-rotating-my-image
        im.save(in_mem_file, format=im.format)
        in_mem_file.seek(0)

        response = client.put_object(
            Body=in_mem_file,
            Bucket=des_bucket,
            Key='resized_' + obj.key
        )

def lambda_handler(event, context):
    bucket = s3.Bucket('myimagebucket0099')

    for obj in bucket.objects.all():
        copy_to_other_bucket(bucket, 'backupimagebucket0099', obj.key)

    resize_image(bucket.name, 'resizedimagebucket0099')


    print(bucket)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-29
    • 2021-04-10
    • 2023-04-11
    • 2011-09-18
    • 1970-01-01
    • 2015-05-19
    • 2011-10-04
    • 1970-01-01
    相关资源
    最近更新 更多