【问题标题】:Creating lambda function to create thumbnail for AWS s3 bucket is not working创建 lambda 函数为 AWS s3 存储桶创建缩略图不起作用
【发布时间】:2018-02-08 16:58:40
【问题描述】:

我正在学习使用 AWS lambda 函数。我想要做的是,当我将图像(jpg)文件上传到 s3 存储桶时,图像将被调整大小。但它不起作用。请问我在下面做了什么。

我创建了一个文件夹。然后在之前创建的文件夹中创建了一个 node_modules 文件夹。然后在主文件夹中创建了一个名为 CreateThumbnail.js 的文件。

这是 CreateThumbnail.js

// 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];
    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");
        }
    );
};

然后我压缩了文件夹。然后我在 AWS lambda 控制台中创建了一个函数,并从 UI 上传 zip 文件,如下所示。

然后我添加了 s3 触发器,如屏幕截图所示。

我还创建了具有正确权限和策略的角色。

但是当我将 JPG 文件上传到 s3 存储桶时,它既没有调整大小,也没有创建缩略图。这里有什么问题?

这是功能策略:

【问题讨论】:

  • 你不需要在s3存储桶上设置读写权限,因为你在向存储桶添加新图像时触发读取,在放入缩略图时触发写入?跨度>
  • 您的意思是向角色添加策略吗?我已经更新了这个问题。请检查是否正确?
  • 你能检查功能策略吗(通过点击 s3 正上方的键 - 在触发器快照中)? S3 应该具有 invokelambda 访问权限。
  • 我刚刚更新了这个问题。请检查。
  • 您可以按照以下步骤操作:Tutorial: Using AWS Lambda with Amazon S3

标签: amazon-web-services amazon-s3 aws-lambda


【解决方案1】:

Lambda 函数将其调试信息发送到 Amazon CloudWatch Logs。检查日志文件以确定出了什么问题。

如果没有日志,则说明 Lambda 函数从未执行,或者 Lambda 函数未获得足够的权限来写入 CloudWatch Logs。

见:Accessing Amazon CloudWatch Logs for AWS Lambda

【讨论】:

    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 2017-12-25
    • 2022-11-03
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2020-05-26
    相关资源
    最近更新 更多