如何使用Multer在Node.js中调整图像大小并上传到S3,并使用easy-image npm模块?

7

我使用Node.js中的easy image npm模块,使用以下代码对图像进行了调整大小。

var easyimg = require('easyimage');

easyimg.rescrop({
     src:'1.jpg', dst:'/var/www/html/bangalore.jpg',
     width:100, height:100

  }),function(image,err){
     // console.log('Resized and cropped: ' + image.width + ' x ' + image.height);
     if(image){
     console.log(image);   
     }
     else{
     console.log(err);    
     }

  }

我已经成功地得到了输出。 然后,使用multer上传了我的图像到s3,代码如下:

var storage = multerS3({
              s3: s3,
              bucket: 'my_bucket_name',
              key: function (req, file, cb) {
                  console.log(file);
                  file_name = file.originalname;
                  var newFileName = Date.now() + "-" + file.originalname;
                  cb(null, newFileName);
              }
           });
              var upload = multer({storage: storage}).single('profileImage');
             upload(req, resq, function (err,res,response) {
              console.log(response);
             });

我的问题是如何在上传到S3之前调整图像大小,然后将调整大小的图像上传到S3?

我还尝试使用 multer-imager 模块。

var transfer = imager({

              secretAccessKey: 'secretAccessKey',
              accessKeyId: 'myaccesskey',
              dirname:'avatar',
              bucket: 'my_bucket',

              region:'myregion',

              key: function (req, file, cb) {
                  console.log(file);
                  file_name = file.originalname;
                  var newFileName = Date.now() + "-" + file.originalname;

                cb(null, newFileName);
                console.log(newFileName);

              },                                    //
    gm: {                                 // [Optional]: define graphicsmagick options
      width: 200,                         // doc: http://aheckmann.github.io/gm/docs.html#resize
      height: 200,
      options: '!',
      format: 'png'                       // Default: jpg
    }
           });
              var upload = multer({storage: transfer}).single('myimage');
             upload(req, resq, function (err,res,response) {
              console.log(req.file); //i am getting this as undefined
             })

但是它不能正常工作。 在'req.file'中,我得到了未定义的结果。

1
为什么不使用https://www.npmjs.com/package/multer-imager? - stdob--
请查看我更新的问题 @stdob - Jagadeesh
不必依赖第三方库,你可以使用 multeraws-sdksharp。这里有一个完整的示例,告诉你如何操作。https://dev59.com/Q7bna4cB1Zd3GeqPXThd#70114704 - ozgrozer
1个回答

14

为什么不使用内置的transforms与multer s3模块一起使用multer-s3-transofrm?

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    shouldTransform: function (req, file, cb) {
      cb(null, /^image/i.test(file.mimetype))
    },
    transforms: [{
      id: 'original',
      key: function (req, file, cb) {
        cb(null, 'image-original.jpg')
      },
      transform: function (req, file, cb) {
        //Perform desired transformations
        cb(null, sharp().resize(600, 600).max())
      }
    }]
  })
})

来自文档:

可选的shouldTransform选项告诉 multer 是否应在上传文件之前转换文件。默认情况下,它设置为 false。如果设置为 true,则必须添加 transforms 选项,该选项告诉如何转换文件。

transforms选项应该是一个数组,包含可以具有属性 id、key 和 transform 的对象。

此示例使用sharp进行转换(一个众所周知的 Node.js 图像处理模块)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接