gm lambda中的错误:string产生空缓冲区

时间:2016-06-10 21:04:16

标签: node.js amazon-s3 buffer aws-lambda gm

我正在尝试使用amazon lambda进行自动定向转换后将缓冲区上传到s3。 我正在从Nodejs api发送缓冲区,如下所示:

var data = request.payload;
          if (data.file) {
            var Media = request.server.plugins.dogwater.media;

            var name = data.file.hapi.filename;
            var local_path = "./uploads/" + name;
            var file = fs.createWriteStream(local_path);
            console.log('adding media...')
            file.on('error', function(err) {
              console.error(err)
            });

            data.file.pipe(file);

            data.file.on('end', function(err) {
              var pic = {
                gallery_id: request.params.gallery_id,
                type: 1
              };
              console.log("\n\n\ndata.file = ",data.file)
  
              Media.create(pic)
                .then(function(media) {
                  httpRequest({
                      url: 'secret',
                      method: 'POST',
                      json: {
                          media_id: media._id,
                          file_name: data.file.hapi.filename,
                          bucket: 'secret',
                          file: data.file
                      },
                      headers: {
                        'x-api-key': 'someapikey'
                      }

当我在api中控制日志data.file._data时,我得到类似的东西:

  _data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48 00 48 00 00 ff e2 0b f8 49 43 43 5f 50 52 4f 46 49 4c 45 00 01 01 00 00 0b e8 00 00 00 00 02 00 00 00 ... >,

在lambda(也是nodejs)中我有这段代码:

// dependencies
var async = require('async');
var AWS = require('aws-sdk');
var gm = require('gm').subClass({
    imageMagick: true
});
var util = require('util');
var path = require('path');

// get reference to S3 client
var s3 = new AWS.S3();
exports.handler = function(event, context) {

    // payload data
    var mediaId = event.media_id;
    var filename = event.file_name;
    var bucket = event.bucket;
    var file = event.file._data;

    console.log("file = ",file);

    // Infer the image type.
    var typeMatch = filename.match(/\.([^.]*)$/);
    if (!typeMatch) {
        console.error('unable to infer image type for file ' + filename);
        return;
    }
    var imageType = typeMatch[1].toLowerCase();

    var acceptedTypes = ['jpg','gif','png','eps','jpeg'];
    if (acceptedTypes.indexOf(imageType)===-1) {
        console.log('skipping non-image ');
        return;
    }


    async.waterfall([
        function process(next) {
            // Transform the image buffer in memory.
            gm(file).autoOrient().toBuffer(
                imageType.toUpperCase(), function(err,
                    buffer) {
                    if (err) {
                        //newly added line below:
                        console.log(err);
                        console.log("hit");
                        next(err);
                    } else {
                        console.log("buffer = ",buffer);
                        next(null,buffer);
                    }
                }
            );
        },
        function upload(data, next) {
            // Stream the image to folder.
            s3.putObject({
                Bucket: bucket,
                Key: mediaId + "/" + filename,
                Body: data,
                ContentType: imageType.toUpperCase()
            }, next);
        }
        ], function(err, result) {
            if (err) {
                console.error(err);
            }
            // return data to api here
            context.done();
        }
    )// end async waterfall
}// end exports handler

因此。当我尝试上传图像时,这就是file._data在gm命令之前打印的内容:

 {type:'Buffer',data:[234,234,234,random numbers etc ...]}

但是在gm命令中,它打印为错误:

  

错误:Stream产生空缓冲区

1 个答案:

答案 0 :(得分:1)

发现问题 -

传入aws lambda的数据必须重新转换为gm接受的缓冲区对象。更改了一行代码(在exports.handler里面,我定义了文件)

&#13;
&#13;
// dependencies
var async = require('async');
var AWS = require('aws-sdk');
var gm = require('gm').subClass({
    imageMagick: true
});
var util = require('util');
var path = require('path');

// get reference to S3 client
var s3 = new AWS.S3();
exports.handler = function(event, context) {

    // payload data
    var mediaId = event.media_id;
    var filename = event.file_name;
    var bucket = event.bucket;
    var file = new Buffer(event.file._data);

    console.log("file = ",file);

    // Infer the image type.
    var typeMatch = filename.match(/\.([^.]*)$/);
    if (!typeMatch) {
        console.error('unable to infer image type for file ' + filename);
        return;
    }
    var imageType = typeMatch[1].toLowerCase();

    var acceptedTypes = ['jpg','gif','png','eps','jpeg'];
    if (acceptedTypes.indexOf(imageType)===-1) {
        console.log('skipping non-image ');
        return;
    }


    async.waterfall([
        function process(next) {
            // Transform the image buffer in memory.
            gm(file).autoOrient().toBuffer(
                imageType.toUpperCase(), function(err,
                    buffer) {
                    if (err) {
                        //newly added line below:
                        console.log(err);
                        console.log("hit");
                        next(err);
                    } else {
                        console.log("buffer = ",buffer);
                        next(null,buffer);
                        //next(null, 'done');
                    }
                }
            );
        },
        function upload(data, next) {
            // Stream the image to folder.
            s3.putObject({
                Bucket: bucket,
                Key: mediaId + "/" + filename,
                Body: data,
                ContentType: imageType.toUpperCase()
            }, next);
        }
        ], function(err, result) {
            if (err) {
                console.error(err);
            }
            // return data to api here
            context.done();
        }
    )// end async waterfall
}// end exports handler
&#13;
&#13;
&#13;