调整节点js中的图像大小

时间:2016-05-10 18:18:27

标签: javascript node.js amazon-web-services amazon-s3 image-upload

我想在将图像上传到s3(亚马逊)之前调整图片大小。 我尝试使用'resizeImg'功能,但它的剂量工作是标准尺寸的图像上传,而不是新尺寸。 我的代码在节点js中写入,然后上传到s3-amazon。 图像名称是:beach_life-normal.jpg

我的代码:

var AWS = require('aws-sdk'),
    fs = require('fs');
var express = require("express");
var app = express();

const resizeImg = require('resize-img');

// For dev purposes only
AWS.config.update({ accessKeyId: 'key', secretAccessKey: 'secret' });

var fileStream = fs.createReadStream('beach_life-normal.jpg');
fileStream.on('error', function (err) {
  if (err) { throw err; }
});  

fileStream.on('open', function () {
  var s3 = new AWS.S3();

resizeImg(fs.readFileSync('beach_life-normal.jpg'), {width: 128, height:  128}).then(buf => {
    fs.writeFileSync('beach_life-normal-new.jpg', buf);
});


  s3.putObject({
    Bucket: 'adinoauploadefile',
    Key: 'beach_life-normal.jpg',
    Body: fileStream
  }, function (err) {
    if (err) { throw err; }
  });

});

1 个答案:

答案 0 :(得分:0)

您应该上传新文件

var AWS = require('aws-sdk'),
fs = require('fs');
var express = require("express");
var app = express();

const resizeImg = require('resize-img');

// For dev purposes only
AWS.config.update({ accessKeyId: 'key', secretAccessKey: 'secret' });

var fileStream = fs.createReadStream('beach_life-normal.jpg');
fileStream.on('error', function (err) {
  if (err) { throw err; }
});  

fileStream.on('open', function () {
  var s3 = new AWS.S3();

resizeImg(fs.readFileSync('beach_life-normal.jpg'), {width: 128, height:  128}).then(buf => {
    fs.writeFileSync('beach_life-normal-new.jpg', buf);
    s3.putObject({
      Bucket: 'adinoauploadefile',
      Key: 'beach_life-normal-new.jpg',
      Body: 'beach_life-normal-new.jpg'
    }, function (err) {
     if (err) { throw err; }
    });
  });  
});