更改AWS Cloudformation模板以为Lambda输出添加S3存储桶

问题描述

我一直在研究CloudFormation和Lambda函数教程,该教程创建了一个S3存储桶和Lambda函数,以将图片的大小调整为缩略图。该教程很有趣,但是我想通过自己进行一些调整并理解更改来更好地理解它以及整个Cloudformation。

当前,使用以下代码,我将图片放入了1个S3存储桶,然后使用称为 schuylerthumbnail-s3-thumbnail-generator 的存储桶将调整后的缩略图放回了其中。 。我想更改代码,以便CloudFormation创建一个额外的存储桶,Lambda函数缩略图放置在那里(代替放置大尺寸图片的相同存储桶)。

Lambda函数

    import boto3
import cStringIO
from PIL import Image,ImageOps
import os

s3 = boto3.client('s3')
size = int(os.environ['THUMBNAIL_SIZE'])


def s3_thumbnail_generator(event,context):
    # parse event
    print(event)
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']
    # only create a thumbnail on non thumbnail pictures
    if (not key.endswith("_thumbnail.png")):
        # get the image
        image = get_s3_image(bucket,key)
        # resize the image
        thumbnail = image_to_thumbnail(image)
        # get the new filename
        thumbnail_key = new_filename(key)
        # upload the file
        url = upload_to_s3(bucket,thumbnail_key,thumbnail)
        return url


def get_s3_image(bucket,key):
    response = s3.get_object(Bucket=bucket,Key=key)
    imagecontent = response['Body'].read()

    file = cStringIO.StringIO(imagecontent)
    img = Image.open(file)
    return img


def image_to_thumbnail(image):
    return ImageOps.fit(image,(size,size),Image.ANTIALIAS)


def new_filename(key):
    key_split = key.rsplit('.',1)
    return key_split[0] + "_thumbnail.png"


def upload_to_s3(bucket,key,image):
    # We're saving the image into a cStringIO object to avoid writing to disk
    out_thumbnail = cStringIO.StringIO()
    # You MUST specify the file type because there is no file name to discern
    # it from
    image.save(out_thumbnail,'PNG')
    out_thumbnail.seek(0)

    response = s3.put_object(
        ACL='public-read',Body=out_thumbnail,Bucket=bucket,ContentType='image/png',Key=key
    )
    print(response)

    url = '{}/{}/{}'.format(s3.Meta.endpoint_url,bucket,key)
    return url

还有yaml文件

 service: python-s3-thumbnail

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"

provider:
  name: aws
  runtime: python2.7
  region: us-east-1
  profile: serverless-admin
  timeout: 10
  memorySize: 128
  iamRoleStatements:
   - Effect: "Allow"
     Action:
       - "s3:*"
     Resource: "*"
  environment:
     THUMBNAIL_SIZE: "128"

custom:
  bucket: schuylerthumbnail-s3-thumbnail-generator
  pythonRequirements:
   dockerizePip: true

functions:
  s3-thumbnail-generator:
    handler: handler.s3_thumbnail_generator
    events:
      - s3:
          bucket: ${self:custom.bucket}
          event: s3:ObjectCreated:*
          rules:
            - suffix: .png

plugins:
  - serverless-python-requirements

    enter code here

存储桶似乎是在yaml文件自定义部分中创建的,我认为我需要在yaml中为第二个存储桶创建一个数组并更改environemnt变量,然后更新Lambda函数中的Python代码是相应的,但不确定。我尝试的修改摘要

custom:
- inbucket: inputthumbnail-s3-thumbnail-generator
  pythonRequirements:
  dockerizePip: true
- outbucket: outputthumbnail-s3-thumbnail-generator
  pythonRequirements:
  dockerizePip: true

但是有点困惑和不确定,这是否是正确的方法以及随后如何使用新的bucket变量正确更改相应的python代码。任何清晰度将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)