Node.js脚本运行一次,然后失败

时间:2015-10-12 22:31:15

标签: javascript node.js amazon-web-services amazon-s3

  

我需要一个执行以下操作的Node.js脚本:

     

1 - 将图像添加到指定的S3存储桶时触发    - 创建该图像的缩略图(360x203像素)。
3 - 将该缩略图的副本保存在单独的S3文件夹中。
4 -   使用a将缩略图上传到指定的FTP服务器,SIX(6)次   “FILENAME-X”命名惯例。

代码首先按预期工作。示例事件会拉动图像。创建一个thumnail。将其保存到其他S3存储桶。然后将其上传到FTP服务器。

问题:它适用于测试文件HappyFace.jpg一次,但随后的每个测试都会失败。此外,我尝试使用不同的文件,但没有成功。

另外:如果我可以帮助编写一个循环来命名上传的不同文件,那将非常感激。我通常用PHP编写代码,所以它可能比我希望写的时间更长。

注意:我删除了我的FTP凭据以保护隐私。

问题代码段:

    function upload(contentType, data, next) {
        // Upload test file to FTP server
        c.append(data, 'testing.jpg', function(err) {
            console.log("CONNECTION SUCCESS!");
            if (err) throw err;
            c.end();
        });
        // Connect to ftp
        c.connect({
            host: "",
            port: 21, // defaults to 21
            user: "", // defaults to "anonymous"
            password: "" // defaults to "@anonymous"
        });
        // S3 Bucket Upload Function Goes Here
    }


完整代码:

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

// get reference to FTP client
var c = new Client();
// 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}));
    // Get source bucket
    var srcBucket = event.Records[0].s3.bucket.name;
    // Get source object key
    // Object key may have spaces or unicode non-ASCII characters.
    var srcKey    =
    decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));  
    var url = 'http://' + srcBucket + ".s3.amazonaws.com/" + srcKey;
    // Set destination bucket
    var dstBucket = srcBucket + "-thumbs";
    // Set destination object key
    var dstKey    = "resized-" + 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 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) {

                // Transform the image buffer in memory.
                this.toBuffer(imageType, function(err, buffer) {
                        if (err) {
                            next(err);
                        } else {
                            next(null, response.ContentType, buffer);
                        }
                    });
            });
        },
        function upload(contentType, data, next) {
            // Upload test file to FTP server
            c.append(data, 'testing.jpg', function(err) {
                console.log("CONNECTION SUCCESS!");
                if (err) throw err;
                c.end();
            });
            // Connect to ftp
            c.connect({
                host: "",
                port: 21, // defaults to 21
                user: "", // defaults to "anonymous"
                password: "" // defaults to "@anonymous"
            });
            // Stream the thumb 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
                );
            }

//          context.done();
        }
    );
};

日志:

START RequestId: edc808c1-712b-11e5-aa8a-ed7c188ee86c Version: $LATEST 
2015-10-12T21:55:20.481Z    edc808c1-712b-11e5-aa8a-ed7c188ee86c    Reading options from event: { Records:  [ { eventVersion: '2.0', eventTime: '1970-01-01T00:00:00.000Z', requestParameters: { sourceIPAddress: '127.0.0.1' }, s3:  { configurationId: 'testConfigRule', object:  { eTag: '0123456789abcdef0123456789abcdef', sequencer: '0A1B2C3D4E5F678901', key: 'HappyFace.jpg', size: 1024 }, bucket:  { arn: 'arn:aws:s3:::images', name: 'images', ownerIdentity: { principalId: 'EXAMPLE' } }, s3SchemaVersion: '1.0' }, responseElements:  { 'x-amz-id-2': 'EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH', 'x-amz-request-id': 'EXAMPLE123456789' }, awsRegion: 'us-east-1', eventName: 'ObjectCreated:Put', userIdentity: { principalId: 'EXAMPLE' }, eventSource: 'aws:s3' } ] } 
2015-10-12T21:55:22.411Z    edc808c1-712b-11e5-aa8a-ed7c188ee86c    Successfully resized images/HappyFace.jpg and uploaded to images-thumbs/resized-HappyFace.jpg 
2015-10-12T21:55:23.432Z    edc808c1-712b-11e5-aa8a-ed7c188ee86c    CONNECTION SUCCESS! 
END RequestId: edc808c1-712b-11e5-aa8a-ed7c188ee86c 
REPORT RequestId: edc808c1-712b-11e5-aa8a-ed7c188ee86c  Duration: 3003.76 ms    Billed Duration: 3000 ms Memory Size: 128 MB    Max Memory Used: 43 MB   
Task timed out after 3.00 seconds  
START RequestId: d347e7e3-712d-11e5-bfdf-05baa36d50fd Version: $LATEST 
2015-10-12T22:08:55.910Z    d347e7e3-712d-11e5-bfdf-05baa36d50fd    Reading options from event: { Records:  [ { eventVersion: '2.0', eventTime: '1970-01-01T00:00:00.000Z', requestParameters: { sourceIPAddress: '127.0.0.1' }, s3:  { configurationId: 'testConfigRule', object:  { eTag: '0123456789abcdef0123456789abcdef', sequencer: '0A1B2C3D4E5F678901', key: 'HappyFace.jpg', size: 1024 }, bucket:  { arn: 'arn:aws:s3:::images', name: 'images', ownerIdentity: { principalId: 'EXAMPLE' } }, s3SchemaVersion: '1.0' }, responseElements:  { 'x-amz-id-2': 'EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH', 'x-amz-request-id': 'EXAMPLE123456789' }, awsRegion: 'us-east-1', eventName: 'ObjectCreated:Put', userIdentity: { principalId: 'EXAMPLE' }, eventSource: 'aws:s3' } ] } 
END RequestId: d347e7e3-712d-11e5-bfdf-05baa36d50fd 
REPORT RequestId: d347e7e3-712d-11e5-bfdf-05baa36d50fd  Duration: 3003.33 ms    Billed Duration: 3000 ms Memory Size: 128 MB    Max Memory Used: 17 MB   
Task timed out after 3.00 seconds

1 个答案:

答案 0 :(得分:2)

该行:

var c = new Client();

只会被执行一次;对handler()函数的所有调用都将使用与FTP客户端相同的实例。

如果可能有多个重叠调用handler() - 并且在异步世界中确实可能 - 则会调用FTP客户端,包括c.connect(…)c.end()多次针对相同的 FTP客户端,可能已经在进行上传,导致出现如下情况:

  1. 致电handler()。上传开始。
  2. 致电handler()。第二次上传开始。
  3. 首次上传完成并调用c.end()
  4. 第二次上传被取消。
  5. 解决方案是为每次上传创建一个新的FTP客户端实例,或者如果您的FTP服务器有问题(限制客户端连接数),您需要以某种方式序列化您的上传。实现这一目标的一种方法是,因为您使用的是async库,所以使用async.queue

相关问题