Reputation:
in my express application, I have my images being saved to a folder in my repo.
However i dont think this is correct i should store them somewhere else, i thought s3 would be a good option.
Ive never done it before so i have no idea how it works.
My currrent code saves the image after scaling it:
exports.resize = async (req, res, next) => {
// check if there is no new file to resize
if (!req.file) {
next(); // skip to the next middleware
return;
}
const extension = req.file.mimetype.split('/')[1]
req.body.photo = `${uuid.v4()}.${extension}`
// now we resize
const photo = await jimp.read(req.file.buffer)
await photo.cover(300, 300);
// await photo.resize(800, jimp.AUTO);
await photo.write(`./public/uploads/${req.body.photo}`);
// once we have written the photo to our filesystem, keep going!
next()
};
Whats the process in order to save the images to aws s3?
Thanks
Upvotes: 1
Views: 16746
Reputation: 2319
Create file upload service
const multer = require('multer');
const multerS3 = require('multer-s3');
const aws = require('aws-sdk');
aws.config.update({
// Your SECRET ACCESS KEY from AWS should go here,
// Never share it!
// Setup Env Variable, e.g: process.env.SECRET_ACCESS_KEY
secretAccessKey: "ab7786ad6",
// Not working key, Your ACCESS KEY ID from AWS should go here,
// Never share it!
// Setup Env Variable, e.g: process.env.ACCESS_KEY_ID
accessKeyId: "ab7786ad6",
region: 'us-east-1' // region of your bucket
});
const s3 = new aws.S3();
create an instance of our Amazon S3.
const upload = multer({
storage: multerS3({
s3: s3,
bucket: 'medium-test',
acl: 'public-read',
metadata: function (req, file, cb) {
cb(null, {fieldName: file.fieldname});
},
key: function (req, file, cb) {
cb(null, Date.now().toString())
}
})
})
module.exports = upload;
Setup a route to upload image
const express = require("express");
const router = express.Router();
const upload = require('../services/multer');
const singleUpload = upload.single('image')
router.post('/image-upload', function(req, res) {
singleUpload(req, res, function(err, some) {
if (err) {
return res.status(422).send({errors: [{title: 'Image Upload Error', detail: err.message}] });
}
return res.json({'imageUrl': req.file.location});
});
})
module.exports = router;
for more reference : https://medium.freecodecamp.org/how-to-set-up-simple-image-upload-with-node-and-aws-s3-84e609248792
Upvotes: 2
Reputation: 8121
You can use the AWS SDK on npm.
From here you will need to configure the package to connect to your buckets..
some useage would be :
const AWS = require('aws-sdk');
const fs = require('fs');
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
\ You could convert the image to base64 in nodeJS for sending later.
And a regular upload method written :
const uploadFile = () => {
fs.readFile(fileName, (err, data) => {
if (err) throw err;
const params = {
Bucket: 'testBucket', // bucket name
Key: 'image.png', // filename
Body: JSON.stringify(data, null, 2)
};
s3.upload(params, function(s3Err, data) {
if (s3Err) throw s3Err;
console.log(`File uploaded successfully at ${data.Location}`);
});
});
};
Ref : AWS Api
Upvotes: 1