如何在nodejs中使用multer-imager npm模块并将我的图像上传到amazon s3 bucket?

时间:2017-07-26 12:23:41

标签: node.js npm multer multer-s3

/********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');



var app = express(),


app.use(bodyParser.json());


var upload = multer({
 storage: imager({
    dirname: 'avatar',
    bucket: 'my bucket',
    accessKeyId: 'myaccesskey',
    secretAccessKey: 'secretaccesskey',
    region: 'Asia Pacific (Mumbai)',
    signatureVersion: 'v4',
    filename: function (req, file, cb) {  
              cb(null, Date.now())                
     },                                    
     gm: {                                  
      width: 200,                         
      height: 200,
      options: '!',
      format: 'png'                       
     },
    s3 : {                                
    Metadata: {                        
    'acl': 'public-read'              
   }
  }

})
});


 app.post('/upload', upload.array('upl', 1), 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文件....

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

当我尝试使用上面的代码上传和调整我的图像大小时,我收到以下错误。但是当我试图直接上传我的文件时没有调整大小它在npm multer-s3模块的帮助下成功但我不能调整大小或缩小缩略图的大小。使用multer s3 module.i需要减少我的缩略图大小可以做什么?

错误:

UnknownEndpoint: Inaccessible host: `s3.asia'. This service may not be available in the `Asia Pacific (Mumbai)' region.
    at Request.ENOTFOUND_ERROR (/path/to/folder//node_modules/s3fs/node_modules/aws-sdk/lib/event_listeners.js:393:46)
    at Request.callListeners (/path/to/folder//node_modules/s3fs/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    at Request.emit (/path/to/folder//node_modules/s3fs/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
    at Request.emit (/path/to/folder//node_modules/s3fs/node_modules/aws-sdk/lib/request.js:668:14)
    at ClientRequest.error (/path/to/folder//node_modules/s3fs/node_modules/aws-sdk/lib/event_listeners.js:232:22)
    at ClientRequest.<anonymous> (/path/to/folder//node_modules/s3fs/node_modules/aws-sdk/lib/http/node.js:62:19)
    at emitOne (events.js:115:13)
    at ClientRequest.emit (events.js:210:7)
    at TLSSocket.socketErrorListener (_http_client.js:399:9)
    at emitOne (events.js:115:13)
    at TLSSocket.emit (events.js:210:7)
    at emitErrorNT (internal/streams/destroy.js:62:8)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickDomainCallback (internal/process/next_tick.js:198:9)

如何解决这个问题?有没有办法让我的地区利用这个功能?

1 个答案:

答案 0 :(得分:1)

您是否尝试过以其他格式添加区域? 我的S3配置看起来像这样:

S3_REGION = EU-中央-1
S3_DOMAIN = https://s3.eu-central-1.amazonaws.com/

(我正在使用aws-sdk模块)