2

I want to resize my images before I upload them to s3 (amazon). I try to use 'resizeImg' function but its dosent work the image upload in standart size, and not in the new size. my code write in node js and then upload to s3-amazon. image name is:beach_life-normal.jpg

My code:

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; }
  });

});
adi
  • 191
  • 2
  • 14

1 Answers1

1

You should upload the new file

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; }
    });
  });  
});
Shimon Tolts
  • 1,602
  • 14
  • 15
  • I tried this code its upload the new image (=new size) but when I open the new image its said : "cant open this image" , ie dosent upload the image correct – adi May 10 '16 at 19:52
  • are you able to open the image locally? are you sure that the issue is not with the resizing? can you check if the MD5 sum of the local file is identical to the uploaded file? – Shimon Tolts May 10 '16 at 21:50
  • yes, I tried to open the image locally and I saw the image – adi May 10 '16 at 21:56