Resizing image with nodeJs and AWS

前端 未结 1 521
盖世英雄少女心
盖世英雄少女心 2021-02-05 19:33

I am attempting to get an image from a AWS S3 bucket using nodejs, resizing it into 4 different sizes and then saving it back to the same bucket but into a folder w

1条回答
  •  离开以前
    2021-02-05 20:05

    Got it working. Main reason was an extra parameter needed to be passed in to the gm(response.Body, srcKey).

    Full code:

    // dependencies
    var async = require('async');
    var AWS = require('aws-sdk');
    var gm = require('gm').subClass({ imageMagick: true });
    var util = require('util');
    
    
    // get reference to S3 client
    var s3 = new AWS.S3();
    
    exports.handler = function(event, context) {
        // 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;
        var srcKey = event.Records[0].s3.object.key;
    
        var _800px = {
            width: 800,
            dstnKey: srcKey,
            destinationPath: "large"
        };
    
        var _500px = {
            width: 500,
            dstnKey: srcKey,
            destinationPath: "medium"
        };
    
        var _200px = {
            width: 200,
            dstnKey: srcKey,
            destinationPath: "small"
        };
    
        var _45px = {
            width: 45,
            dstnKey: srcKey,
            destinationPath: "thumbnail"
        };
    
        var _sizesArray = [_800px, _500px, _200px, _45px];
    
        var len = _sizesArray.length;
    
        console.log(len);
        console.log(srcBucket);
        console.log(srcKey);
    
        // Infer the image type.
        var typeMatch = srcKey.match(/\.([^.]*)$/);
        if (!typeMatch) {
            console.error('unable to infer image type for key ' + srcKey);
            return;
        }
        var imageType = typeMatch[1];
        if (imageType != "jpg" && imageType != "png") {
            console.log('skipping non-image ' + srcKey);
            return;
        }
    
        // Download the image from S3, transform, and upload to same S3 bucket but different folders.
        async.waterfall([
                function download(next) {
                    // Download the image from S3 into a buffer.
    
                    s3.getObject({
                            Bucket: srcBucket,
                            Key: srcKey
                        },
                        next);
                },
    
                function transform(response, next) {
    
    
                    for (var i = 0; iUnable to resize ' + srcBucket + '/' + srcKey +
                        ' and upload to ' + srcBucket + '/dst' +
                        ' due to an error: ' + err
                    );
                } else {
                    console.log(
                        '---->Successfully resized ' + srcBucket +
                        ' and uploaded to' + srcBucket + "/dst"
                    );
                }
    
                context.done();
            }
        );
    };
    

    0 讨论(0)
提交回复
热议问题