示例 Amazon S3 函数代码 - AWS Lambda
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

示例 Amazon S3 函数代码

示例代码具有以下语言。

Node.js 12.x

以下示例代码接收 Amazon S3 事件输入并对其所包含的消息进行处理。它调整源存储桶中图像的大小并将输出保存到目标存储桶。

例 index.js

// dependencies const AWS = require('aws-sdk'); const util = require('util'); const sharp = require('sharp'); // get reference to S3 client const s3 = new AWS.S3(); exports.handler = async (event, context, callback) => { // Read options from the event parameter. console.log("Reading options from event:\n", util.inspect(event, {depth: 5})); const srcBucket = event.Records[0].s3.bucket.name; // Object key may have spaces or unicode non-ASCII characters. const srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")); const dstBucket = srcBucket + "-resized"; const dstKey = "resized-" + srcKey; // Infer the image type from the file suffix. const typeMatch = srcKey.match(/\.([^.]*)$/); if (!typeMatch) { console.log("Could not determine the image type."); return; } // Check that the image type is supported const imageType = typeMatch[1].toLowerCase(); if (imageType != "jpg" && imageType != "png") { console.log(`Unsupported image type: ${imageType}`); return; } // Download the image from the S3 source bucket. try { const params = { Bucket: srcBucket, Key: srcKey }; var origimage = await s3.getObject(params).promise(); } catch (error) { console.log(error); return; } // set thumbnail width. Resize will set the height automatically to maintain aspect ratio. const width = 200; // Use the Sharp module to resize the image and save in a buffer. try { var buffer = await sharp(origimage.Body).resize(width).toBuffer(); } catch (error) { console.log(error); return; } // Upload the thumbnail image to the destination bucket try { const destparams = { Bucket: dstBucket, Key: dstKey, Body: buffer, ContentType: "image" }; const putResult = await s3.putObject(destparams).promise(); } catch (error) { console.log(error); return; } console.log('Successfully resized ' + srcBucket + '/' + srcKey + ' and uploaded to ' + dstBucket + '/' + dstKey); };

部署程序包是包含 Lambda 函数代码和依赖项的 .zip 文件。

创建部署程序包

  1. 创建文件夹 (examplefolder),然后创建子文件夹 (node_modules)。

  2. 安装依赖项。本代码示例使用以下库:

    • 适用于 Node.js 中 JavaScript 的 AWS 开发工具包

    • 适用于 node.js 的 Sharp

    AWS Lambda 运行时已具有用 Node.js 编写的适用于 JavaScript 的 AWS 开发工具包,因此您只需安装 Sharp 库。打开命令提示符,导航到 examplefolder,使用 npm 命令(Node.js 的一部分)安装这些库。对于 Linux,请使用以下命令。

    $ npm install sharp

    对于 macOS,请使用以下命令。

    $ npm install --arch=x64 --platform=linux --target=12.13.0 sharp
  3. 将示例代码保存到名为 index.js 的文件中。

  4. 查看上述代码并注意以下内容:

    • 函数通过作为参数接收的事件数据获知源存储桶名称和对象键名称。如果对象为 .jpg,则该代码会创建一个缩略图并将其保存到目标存储桶。

    • 该代码假定目标存储桶已存在,且其名称为源存储桶名称后跟字符串 -resized。例如,如果在事件数据中识别的源存储桶为 examplebucket,则代码假定您具有目标存储桶 examplebucket-resized

    • 对于所创建的缩略图,该代码会将其键名称派生为后跟源对象键名称的字符串 resized-。例如,如果源对象键为 sample.jpg,则代码会创建具有键 resized-sample.jpg 的缩略图对象。

  5. 将该文件保存到 index.js 中,文件名为 examplefolder。完成此步骤后,文件夹结构如下:

    index.js /node_modules/sharp
  6. 将 index.js 文件和 node_modules 文件夹压缩为 CreateThumbnail.zip

Java 11

下面是读取传入的 Amazon S3 事件并创建缩略图的示例 Java 代码。请注意,它实现了 aws-lambda-java-core 库中提供的 RequestHandler 接口。因此,在创建 Lambda 函数时,您可以将该类指定为处理程序(即 example.handler)。有关使用接口提供处理程序的更多信息,请参阅处理程序接口

被该处理程序用作输入类型的 S3Event 类型是 aws-lambda-java-events 库中的一个预定义类,它为您提供了一些方便地从传入的 Amazon S3 事件读取信息的方法。该处理程序返回字符串作为输出。

例 Handler.java

package example; import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.imageio.ImageIO; import com.amazonaws.AmazonServiceException; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.events.S3Event; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.AmazonS3ClientBuilder; public class Handler implements RequestHandler<S3Event, String> { private static final float MAX_WIDTH = 100; private static final float MAX_HEIGHT = 100; private final String JPG_TYPE = (String) "jpg"; private final String JPG_MIME = (String) "image/jpeg"; private final String PNG_TYPE = (String) "png"; private final String PNG_MIME = (String) "image/png"; public String handleRequest(S3Event s3event, Context context) { try { S3EventNotificationRecord record = s3event.getRecords().get(0); String srcBucket = record.getS3().getBucket().getName(); // Object key may have spaces or unicode non-ASCII characters. String srcKey = record.getS3().getObject().getUrlDecodedKey(); String dstBucket = srcBucket + "-resized"; String dstKey = "resized-" + srcKey; // Sanity check: validate that source and destination are different // buckets. if (srcBucket.equals(dstBucket)) { System.out .println("Destination bucket must not match source bucket."); return ""; } // Infer the image type. Matcher matcher = Pattern.compile(".*\\.([^\\.]*)").matcher(srcKey); if (!matcher.matches()) { System.out.println("Unable to infer image type for key " + srcKey); return ""; } String imageType = matcher.group(1); if (!(JPG_TYPE.equals(imageType)) && !(PNG_TYPE.equals(imageType))) { System.out.println("Skipping non-image " + srcKey); return ""; } // Download the image from S3 into a stream AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); S3Object s3Object = s3Client.getObject(new GetObjectRequest( srcBucket, srcKey)); InputStream objectData = s3Object.getObjectContent(); // Read the source image BufferedImage srcImage = ImageIO.read(objectData); int srcHeight = srcImage.getHeight(); int srcWidth = srcImage.getWidth(); // Infer the scaling factor to avoid stretching the image // unnaturally float scalingFactor = Math.min(MAX_WIDTH / srcWidth, MAX_HEIGHT / srcHeight); int width = (int) (scalingFactor * srcWidth); int height = (int) (scalingFactor * srcHeight); BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = resizedImage.createGraphics(); // Fill with white before applying semi-transparent (alpha) images g.setPaint(Color.white); g.fillRect(0, 0, width, height); // Simple bilinear resize g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(srcImage, 0, 0, width, height, null); g.dispose(); // Re-encode image to target format ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(resizedImage, imageType, os); InputStream is = new ByteArrayInputStream(os.toByteArray()); // Set Content-Length and Content-Type ObjectMetadata meta = new ObjectMetadata(); meta.setContentLength(os.size()); if (JPG_TYPE.equals(imageType)) { meta.setContentType(JPG_MIME); } if (PNG_TYPE.equals(imageType)) { meta.setContentType(PNG_MIME); } // Uploading to S3 destination bucket System.out.println("Writing to: " + dstBucket + "/" + dstKey); try { s3Client.putObject(dstBucket, dstKey, is, meta); } catch(AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Successfully resized " + srcBucket + "/" + srcKey + " and uploaded to " + dstBucket + "/" + dstKey); return "Ok"; } catch (IOException e) { throw new RuntimeException(e); } } }

Amazon S3 使用 Event调用类型调用您的 Lambda 函数,其中 AWS Lambda 以异步方式执行代码。返回什么不重要。但在本示例中,我们实现的接口要求指定返回类型,因此,本示例中的处理程序使用了 String 作为返回类型。

附属物

  • aws-lambda-java-core

  • aws-lambda-java-events

  • aws-java-sdk

使用 Lambda 库依赖项构建代码以创建部署程序包。有关说明,请参阅Java 中的 AWS Lambda 部署程序包

Python 3

以下示例代码接收 Amazon S3 事件输入并对其所包含的消息进行处理。它调整源存储桶中图像的大小并将输出保存到目标存储桶。

例 lambda_function.py

import boto3 import os import sys import uuid from urllib.parse import unquote_plus 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 lambda_handler(event, context): for record in event['Records']: bucket = record['s3']['bucket']['name'] key = unquote_plus(record['s3']['object']['key']) tmpkey = key.replace('/', '') download_path = '/tmp/{}{}'.format(uuid.uuid4(), tmpkey) upload_path = '/tmp/resized-{}'.format(tmpkey) s3_client.download_file(bucket, key, download_path) resize_image(download_path, upload_path) s3_client.upload_file(upload_path, '{}-resized'.format(bucket), key)
注意

此代码使用的映像库必须安装在 Linux 环境中,才能创建有效的部署包。

创建部署程序包

  1. 将示例代码复制到名为 lambda_function.py 的文件中。

  2. 创建虚拟环境。

    s3-python$ virtualenv v-env s3-python$ source v-env/bin/activate
  3. 在虚拟环境中安装库

    (v-env) s3-python$ pip install Pillow boto3
  4. 使用已安装库的内容创建部署包。

    (v-env) s3-python$ cd $VIRTUAL_ENV/lib/python3.8/site-packages (v-env) python-s3/v-env/lib/python3.8/site-packages$ zip -r9 ${OLDPWD}/function.zip .
  5. 将处理程序代码添加到部署包并停用虚拟环境。

    (v-env) python-s3/v-env/lib/python3.8/site-packages$ cd ${OLDPWD} (v-env) python-s3$ zip -g function.zip lambda_function.py adding: lambda_function.py (deflated 55%) (v-env) python-s3$ deactivate