lundi 12 octobre 2015

Using Node.js to upload S3 object to FTP server

I need a Node.js script that does the following:

1 - Triggers when an image is added to a specified S3 bucket.
2 - Creates a thumbnail of that image (360x203 pixels).
3 - Saves a copy of that thumbnail inside of a separate S3 folder.
4 - Uploads the thumbnail to a specified FTP server, SIX (6) times using a "FILENAME-X"naming convention.

The problem: I am not able to get the c.connect or c.append to work. I have tried about everything, and scoured the internet. It seems like it should work, but just doesn't.

Note: I removed my FTP credentials for privacy.

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) {
            // Connect to server
            c.connect({
                host: "localhost",
                port: 21, // defaults to 21
                user: "", // defaults to "anonymous"
                password: "", // defaults to "@anonymous"
            });
            // Upload test file to FTP server
            c.append(data, srcKey, function(err) {
                console.log("CONNECTION SUCCESS!");
                if (err) throw err;
                c.end();
            });
            // 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();
        }
    );
};




Aucun commentaire:

Enregistrer un commentaire