【问题标题】:TypeError: Cannot read property 'width' of undefined gm libraryTypeError:无法读取未定义 gm 库的属性“宽度”
【发布时间】:2019-10-28 21:01:02
【问题描述】:

之前,我曾按照link 使用 AWS Lambda 成功创建缩略图。节点版本为 8.10

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

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

发生了什么?

Node 社区已决定终止对 Node.js 8.x 的支持 2019 年 12 月 31 日1。从今天开始,Node.js 8.x 将停止 接收错误修复、安全更新和/或性能 改进。确保您的新功能和现有功能在 支持和安全的运行时,已达到的语言运行时 他们的 EOL 在 AWS 2 中已弃用。

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

  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的内存大小

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

【问题讨论】:

    标签: node.js aws-lambda imagemagick gm


    【解决方案1】:

    问题是,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

    【讨论】:

    • 只是为了完成答案:您现在应该使用 Sharp 进行图像处理。
    猜你喜欢
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 2019-06-21
    相关资源
    最近更新 更多