Uploading an image in s3 using nodejs












0















I'm trying to upload an image to s3 using node js. I'm using multer-s3.
When I try to open the image from s3 it shows"Preview not available.Your file may be corrupted".I tried fs too but still the same result.



services/file-upload.js

const s3Config = new aws.S3({
secretAccessKey: config.awsCredentials.secretAccessKey,
accessKeyId: config.awsCredentials.accessKeyId,
region: config.awsCredentials.region,
bucket: 'bucketname',
});

const fileFilter = (req, file, cb) => {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, true)
} else {
cb(null, false)
}
}

const multerS3Config = multerS3({
s3: s3Config,
bucket: 'bucketname',
metadata: function (req, file, cb) {
cb(null, { fieldName: file.fieldname });
},
key: function (req, file, cb) {
console.log(file)
cb(null, new Date().toISOString() + '-' + file.originalname)
}
});

const upload = multer({
storage: multerS3Config,
fileFilter: fileFilter,
limits: {
fileSize: 1024 * 1024 * 5 // we are allowing only 5 MB files
}
})
controller.js
const upload = require('../services/file-upload');
var Uploader = require('s3-image-uploader');
const singleUpload = upload.single('file');
singleUpload(req, res, function(err) {
if (err) {
return res.status(422).send({errors: [{title: 'Image Upload Error', detail: err.message}] });
}

return res.json({'imageUrl': "uploaded successfully"});
});









share|improve this question























  • Is the file is greater than 5MB?

    – Sayed Mohd Ali
    Nov 14 '18 at 12:30











  • no it is less than 5MB

    – Thara
    Nov 14 '18 at 12:41











  • Previously I was having the same problem with file uploading on Node.js using Multer and fs but it is for the Angular.js and Node.js RestAPI. I think it may also help you out with S3. You need to add transformRequest: follow this link for explaination stackoverflow.com/questions/39284412/…

    – Sayed Mohd Ali
    Nov 14 '18 at 12:48











  • if possible can you share the code.it would be a great help

    – Thara
    Nov 14 '18 at 12:52











  • when I used this library it worked fine with me without any error. clone github.com/rahil471/file-upload-with-angularjs-and-nodejs.git

    – Sayed Mohd Ali
    Nov 14 '18 at 13:00
















0















I'm trying to upload an image to s3 using node js. I'm using multer-s3.
When I try to open the image from s3 it shows"Preview not available.Your file may be corrupted".I tried fs too but still the same result.



services/file-upload.js

const s3Config = new aws.S3({
secretAccessKey: config.awsCredentials.secretAccessKey,
accessKeyId: config.awsCredentials.accessKeyId,
region: config.awsCredentials.region,
bucket: 'bucketname',
});

const fileFilter = (req, file, cb) => {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, true)
} else {
cb(null, false)
}
}

const multerS3Config = multerS3({
s3: s3Config,
bucket: 'bucketname',
metadata: function (req, file, cb) {
cb(null, { fieldName: file.fieldname });
},
key: function (req, file, cb) {
console.log(file)
cb(null, new Date().toISOString() + '-' + file.originalname)
}
});

const upload = multer({
storage: multerS3Config,
fileFilter: fileFilter,
limits: {
fileSize: 1024 * 1024 * 5 // we are allowing only 5 MB files
}
})
controller.js
const upload = require('../services/file-upload');
var Uploader = require('s3-image-uploader');
const singleUpload = upload.single('file');
singleUpload(req, res, function(err) {
if (err) {
return res.status(422).send({errors: [{title: 'Image Upload Error', detail: err.message}] });
}

return res.json({'imageUrl': "uploaded successfully"});
});









share|improve this question























  • Is the file is greater than 5MB?

    – Sayed Mohd Ali
    Nov 14 '18 at 12:30











  • no it is less than 5MB

    – Thara
    Nov 14 '18 at 12:41











  • Previously I was having the same problem with file uploading on Node.js using Multer and fs but it is for the Angular.js and Node.js RestAPI. I think it may also help you out with S3. You need to add transformRequest: follow this link for explaination stackoverflow.com/questions/39284412/…

    – Sayed Mohd Ali
    Nov 14 '18 at 12:48











  • if possible can you share the code.it would be a great help

    – Thara
    Nov 14 '18 at 12:52











  • when I used this library it worked fine with me without any error. clone github.com/rahil471/file-upload-with-angularjs-and-nodejs.git

    – Sayed Mohd Ali
    Nov 14 '18 at 13:00














0












0








0








I'm trying to upload an image to s3 using node js. I'm using multer-s3.
When I try to open the image from s3 it shows"Preview not available.Your file may be corrupted".I tried fs too but still the same result.



services/file-upload.js

const s3Config = new aws.S3({
secretAccessKey: config.awsCredentials.secretAccessKey,
accessKeyId: config.awsCredentials.accessKeyId,
region: config.awsCredentials.region,
bucket: 'bucketname',
});

const fileFilter = (req, file, cb) => {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, true)
} else {
cb(null, false)
}
}

const multerS3Config = multerS3({
s3: s3Config,
bucket: 'bucketname',
metadata: function (req, file, cb) {
cb(null, { fieldName: file.fieldname });
},
key: function (req, file, cb) {
console.log(file)
cb(null, new Date().toISOString() + '-' + file.originalname)
}
});

const upload = multer({
storage: multerS3Config,
fileFilter: fileFilter,
limits: {
fileSize: 1024 * 1024 * 5 // we are allowing only 5 MB files
}
})
controller.js
const upload = require('../services/file-upload');
var Uploader = require('s3-image-uploader');
const singleUpload = upload.single('file');
singleUpload(req, res, function(err) {
if (err) {
return res.status(422).send({errors: [{title: 'Image Upload Error', detail: err.message}] });
}

return res.json({'imageUrl': "uploaded successfully"});
});









share|improve this question














I'm trying to upload an image to s3 using node js. I'm using multer-s3.
When I try to open the image from s3 it shows"Preview not available.Your file may be corrupted".I tried fs too but still the same result.



services/file-upload.js

const s3Config = new aws.S3({
secretAccessKey: config.awsCredentials.secretAccessKey,
accessKeyId: config.awsCredentials.accessKeyId,
region: config.awsCredentials.region,
bucket: 'bucketname',
});

const fileFilter = (req, file, cb) => {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, true)
} else {
cb(null, false)
}
}

const multerS3Config = multerS3({
s3: s3Config,
bucket: 'bucketname',
metadata: function (req, file, cb) {
cb(null, { fieldName: file.fieldname });
},
key: function (req, file, cb) {
console.log(file)
cb(null, new Date().toISOString() + '-' + file.originalname)
}
});

const upload = multer({
storage: multerS3Config,
fileFilter: fileFilter,
limits: {
fileSize: 1024 * 1024 * 5 // we are allowing only 5 MB files
}
})
controller.js
const upload = require('../services/file-upload');
var Uploader = require('s3-image-uploader');
const singleUpload = upload.single('file');
singleUpload(req, res, function(err) {
if (err) {
return res.status(422).send({errors: [{title: 'Image Upload Error', detail: err.message}] });
}

return res.json({'imageUrl': "uploaded successfully"});
});






node.js amazon-s3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 11:18









TharaThara

62




62













  • Is the file is greater than 5MB?

    – Sayed Mohd Ali
    Nov 14 '18 at 12:30











  • no it is less than 5MB

    – Thara
    Nov 14 '18 at 12:41











  • Previously I was having the same problem with file uploading on Node.js using Multer and fs but it is for the Angular.js and Node.js RestAPI. I think it may also help you out with S3. You need to add transformRequest: follow this link for explaination stackoverflow.com/questions/39284412/…

    – Sayed Mohd Ali
    Nov 14 '18 at 12:48











  • if possible can you share the code.it would be a great help

    – Thara
    Nov 14 '18 at 12:52











  • when I used this library it worked fine with me without any error. clone github.com/rahil471/file-upload-with-angularjs-and-nodejs.git

    – Sayed Mohd Ali
    Nov 14 '18 at 13:00



















  • Is the file is greater than 5MB?

    – Sayed Mohd Ali
    Nov 14 '18 at 12:30











  • no it is less than 5MB

    – Thara
    Nov 14 '18 at 12:41











  • Previously I was having the same problem with file uploading on Node.js using Multer and fs but it is for the Angular.js and Node.js RestAPI. I think it may also help you out with S3. You need to add transformRequest: follow this link for explaination stackoverflow.com/questions/39284412/…

    – Sayed Mohd Ali
    Nov 14 '18 at 12:48











  • if possible can you share the code.it would be a great help

    – Thara
    Nov 14 '18 at 12:52











  • when I used this library it worked fine with me without any error. clone github.com/rahil471/file-upload-with-angularjs-and-nodejs.git

    – Sayed Mohd Ali
    Nov 14 '18 at 13:00

















Is the file is greater than 5MB?

– Sayed Mohd Ali
Nov 14 '18 at 12:30





Is the file is greater than 5MB?

– Sayed Mohd Ali
Nov 14 '18 at 12:30













no it is less than 5MB

– Thara
Nov 14 '18 at 12:41





no it is less than 5MB

– Thara
Nov 14 '18 at 12:41













Previously I was having the same problem with file uploading on Node.js using Multer and fs but it is for the Angular.js and Node.js RestAPI. I think it may also help you out with S3. You need to add transformRequest: follow this link for explaination stackoverflow.com/questions/39284412/…

– Sayed Mohd Ali
Nov 14 '18 at 12:48





Previously I was having the same problem with file uploading on Node.js using Multer and fs but it is for the Angular.js and Node.js RestAPI. I think it may also help you out with S3. You need to add transformRequest: follow this link for explaination stackoverflow.com/questions/39284412/…

– Sayed Mohd Ali
Nov 14 '18 at 12:48













if possible can you share the code.it would be a great help

– Thara
Nov 14 '18 at 12:52





if possible can you share the code.it would be a great help

– Thara
Nov 14 '18 at 12:52













when I used this library it worked fine with me without any error. clone github.com/rahil471/file-upload-with-angularjs-and-nodejs.git

– Sayed Mohd Ali
Nov 14 '18 at 13:00





when I used this library it worked fine with me without any error. clone github.com/rahil471/file-upload-with-angularjs-and-nodejs.git

– Sayed Mohd Ali
Nov 14 '18 at 13:00












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298996%2fuploading-an-image-in-s3-using-nodejs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298996%2fuploading-an-image-in-s3-using-nodejs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python