2

在使用 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 命令中,这将打印为错误:

错误:流产生空缓冲区

4

1 回答 1

2

发现了问题——

传递给 aws lambda 的数据必须重新制作成一个缓冲区对象才能被 gm 接受。更改了一行代码(在exports.handler 中,我在其中定义文件)

// 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

于 2016-06-13T02:50:23.063 回答