问题
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; }
});
});
回答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; }
});
});
});
来源:https://stackoverflow.com/questions/37146106/resize-image-in-node-js