1

以前,我已按照此链接使用 AWS Lambda 成功创建缩略图。节点版本是8.10

现在,由于 AWS 将弃用具有此节点版本的任何应用程序,我必须将节点版本更新为10.x

我们正在与您联系,因为我们发现您的 AWS 账户目前有一个或多个使用 Node.js 8.10 的 Lambda 函数,该函数将在 2019 年底达到其 EOL。

发生了什么?

Node 社区已决定于 2019 年 12 月 31 日结束对 Node.js 8.x 的支持1。从该日期开始,Node.js 8.x 将停止接收错误修复、安全更新和/或性能改进。为确保您的新函数和现有函数在受支持且安全的运行时上运行,已达到其 EOL 的语言运行时在 AWS 2中已弃用。

对于 Node.js 8.x,运行时弃用过程将分为 2 个阶段:

  1. 禁用函数创建 - 从 2020 年 1 月 6 日开始,客户将不再能够使用 Node.js 8.10 创建函数

  2. 禁用函数更新 - 从 2020 年 2 月 3 日开始,客户将无法再使用 Node.js 8.10 更新函数

在此期间之后,功能创建和更新都将被永久禁用。但是,现有的 Node 8.x 函数仍可用于处理调用事件。

我需要做什么?

我们鼓励您将所有 Node.js 8.10 函数更新到更新的可用运行时版本 Node.js 10.x[3]。在将更改应用于生产函数之前,您应该测试函数与 Node.js 10.x 语言版本的兼容性。

如果我有问题怎么办/如果我需要帮助怎么办?

如果您有任何问题或疑虑,请通过 AWS Support [4] 或 AWS 开发人员论坛 [5] 联系我们。

因此,我已将我的节点版本更新为10.17.0并在 AWS Lambda 中再次部署该程序包。现在,如果任何图像已上传到 S3 并且 aws lambda 尝试将图像转换为缩略图,则会显示以下错误:

在此处输入图像描述

这是完整的代码:

// dependencies
var async = require('async');
var AWS = require('aws-sdk');
var gm = require('gm')
            .subClass({ imageMagick: true }); // Enable ImageMagick integration.
var util = require('util');

// constants
var MAX_WIDTH  = 100;
var MAX_HEIGHT = 100;

// get reference to S3 client 
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");
        }
    );
};

错误基本上在这一行:

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, // In this line where the size is undefined
                    MAX_HEIGHT / size.height
                );

8.10 的节点版本运行良好。我不知道在这种情况下该怎么做。我在配置中有1024MB的内存大小

谁能指出我应该在哪里改变?提前致谢。

4

1 回答 1

4

问题是,NodeJS 10.x 不再支持 gm:https ://github.com/aheckmann/gm/issues/752 。

所以这里会有问题。似乎您必须创建并使用 Lambda 层来修复它。但是,如果您使用的是 Lambda@Edge,就会出现真正的问题,因为此处不允许使用 Lambda 层:https ://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html

于 2019-10-28T13:01:02.743 回答