AWS Lambda
开发人员指南
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

示例 Amazon Simple Storage Service 函数代码

示例代码具有以下语言。

Node.js 8

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

例 index.js

var async = require('async'); var AWS = require('aws-sdk'); var gm = require('gm') .subClass({ imageMagick: true }); // Enable ImageMagick integration. var util = require('util'); var MAX_WIDTH = 100; var MAX_HEIGHT = 100; var s3 = new AWS.S3(); exports.handler = function(event, context, callback) { // Read options from the event. console.log("Reading options from event:\n", util.inspect(event, {depth: 5})); var srcBucket = event.Records[0].s3.bucket.name; // Object key may have spaces or unicode non-ASCII characters. var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")); var dstBucket = srcBucket + "resized"; var dstKey = "resized-" + srcKey; // Sanity check: validate that source and destination are different buckets. if (srcBucket == dstBucket) { callback("Source and destination buckets are the same."); return; } // Infer the image type. var typeMatch = srcKey.match(/\.([^.]*)$/); if (!typeMatch) { callback("Could not determine the image type."); return; } var imageType = typeMatch[1].toLowerCase(); if (imageType != "jpg" && imageType != "png") { callback(`Unsupported image type: ${imageType}`); return; } // Download the image from S3, transform, and upload to a different S3 bucket. async.waterfall([ function download(next) { // Download the image from S3 into a buffer. s3.getObject({ Bucket: srcBucket, Key: srcKey }, next); }, function transform(response, next) { gm(response.Body).size(function(err, size) { // Infer the scaling factor to avoid stretching the image unnaturally. var scalingFactor = Math.min( MAX_WIDTH / size.width, MAX_HEIGHT / size.height ); var width = scalingFactor * size.width; var height = scalingFactor * size.height; // Transform the image buffer in memory. this.resize(width, height) .toBuffer(imageType, function(err, buffer) { if (err) { next(err); } else { next(null, response.ContentType, buffer); } }); }); }, function upload(contentType, data, next) { // Stream the transformed image to a different S3 bucket. s3.putObject({ Bucket: dstBucket, Key: dstKey, Body: data, ContentType: contentType }, next); } ], function (err) { if (err) { console.error( 'Unable to resize ' + srcBucket + '/' + srcKey + ' and upload to ' + dstBucket + '/' + dstKey + ' due to an error: ' + err ); } else { console.log( 'Successfully resized ' + srcBucket + '/' + srcKey + ' and uploaded to ' + dstBucket + '/' + dstKey ); } callback(null, "message"); } ); };

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

创建部署程序包

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

  2. 安装 Node.js 平台。有关更多信息,请参阅 Node.js 网站。

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

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

    • gm,GraphicsMagick for node.js

    • Async 实用程序模块

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

    $ npm install async gm
  4. 将示例代码保存到名为 index.js 的文件中。

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

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

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

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

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

    index.js /node_modules/gm /node_modules/async
  7. 将 index.js 文件和 node_modules 文件夹压缩为 CreateThumbnail.zip

Java 8

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

被该处理程序用作输入类型的 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 事件输入并对其所包含的消息进行处理。它调整源存储桶中图像的大小并将输出保存到目标存储桶。

例 CreateThumbnail.py

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(tuple(x / 2 for x in image.size)) image.save(resized_path) 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) s3_client.download_file(bucket, key, download_path) resize_image(download_path, upload_path) s3_client.upload_file(upload_path, '{}resized'.format(bucket), key)

创建部署程序包

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

  2. 创建虚拟环境。

    $ virtualenv ~/shrink_venv

    $ source ~/shrink_venv/bin/activate

  3. 在虚拟环境中安装库

    $ pip install Pillow

    $ pip install boto3

  4. liblib64 站点程序包的内容添加到 .zip 文件中。

    $ cd $VIRTUAL_ENV/lib/python3.7/site-packages

    $ zip -r ~/CreateThumbnail.zip .

  5. 添加 Python 代码到 .zip 文件

    $ cd ~

    $ zip -g CreateThumbnail.zip CreateThumbnail.py

本页内容: