尝试在nodejs中使用multer调整大小和上传时,在s3中创建零字节文件?

时间:2017-07-31 04:35:22

标签: node.js amazon-s3 multer multer-s3

下面是我的app.js文件..每当我尝试使用multer-imager模块调整大小并上传我的图像时,每次都会创建一个零字节文件,我没有得到任何响应(继续加载后期操作)

/*********app.js*********/


var express = require('express'),
    aws = require('aws-sdk'),
    bodyParser = require('body-parser'),
    multer = require('multer'),
    imager = require('multer-imager'),
    multerS3 = require('multer-s3');
    gm = require('gm');
var Upload = require('s3-uploader');



var app = express(),
    s3 = new aws.S3();

app.use(bodyParser.json());


var upload = multer({
  storage: imager({
    dirname: 'directory',
    bucket: 'bucket',
    accessKeyId: 'accessKeyId',
    secretAccessKey: 'my secretAccessKey',
    region: 'my region',
    filename: function (req, file, cb) {  // [Optional]: define filename (default: random)
      cb(null, Date.now())                // i.e. with a timestamp
    },                                    //
    gm: {                                 // [Optional]: define graphicsmagick options
      width: 200,                         // doc: http://aheckmann.github.io/gm/docs.html#resize
      height: 200,
      options: '!',
      format: 'png'                       // Default: jpg
    },
    s3 : {                                // [Optional]: define s3 options
      Metadata: {                         // http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
        'customkey': 'data'               // "x-amz-meta-customkey","value":"data"
      }
    }
  })
});

 app.post('/upload', upload.any(), function(req, res, next){ 
 console.log(req.files); // Print upload details
 res.send('Successfully uploaded!');
 }); 


app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});



app.listen(3001, function () {
    console.log('Example app listening on port 3001!');
});

以下是我的index.html文件。

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
Hey! Lets try uploading to s3 directly :)

<form method="post" enctype="multipart/form-data" action="/upload">
    <p>
        <input type="text" name="title" placeholder="optional title"/>
    </p>

    <p>
        <input type="file" name="upl"/>
        <!-- <input type="file" name="uplo"/> -->
    </p>

    <p>
        <input type="submit"/>
    </p>
</form>
</body>
</html>

但我可以使用multer-s3模块上传图像而无需进行任何修改。但是我必须调整大小。帮助我纠正这些错误。

1 个答案:

答案 0 :(得分:1)

我认为您的系统中没有安装GraphicsMagick软件包(非NPM软件包)。

请浏览GraphicsMagick指南并在系统中安装GraphicsMagick

http://www.graphicsmagick.org/README.html