I am using the NPM documentation for uploading an image to Google Cloud Storage
app.post('/test', upload.single('image'), function (req, res, next) {
});
let bucket = gcs.bucket('bucket-name');
bucket.upload(req.file.path, function(err, file) {
if (err) {
throw new Error(err);
}
console.log(file);
});
console.log(req.file);
// req.body will hold the text fields, if there were any
})
Is there any way I can specify a custom name and file type for the file I'm uploading? In my case it should be an image.
You can pass options to your upload method:
var options = {
destination: 'my-image-name.jpg',
metadata: {
contentType: 'image/jpeg',
}
};
bucket.upload(req.file.path, options, function(err, file) {
});
Related
I want to send some image file from one Nodejs server to another Nodejs server. And how to get the file in second server? Also how to save into a folder in second server?
How to do that any suggestion?
First server
uploadImage(req, callback) {
var formData = new FormData();
var body = {
"file": req.file,
}
var options = {
'method': 'POST',
'url': config.db_layer_endpointUpload,
'headers': {
'api_key': config.db_layer_access_key,
'content-type': 'application/json'
},
body: JSON.stringify(body),
}
request(options, function (error, response) {
return callback(response.body);
})
}
Second server
app.post(
"/upload",
multerObj.single("file"),
(req, res) => {
console.log(req.body);
}
);
When console.log i am getting following result in Second server file
But Image is not saved in the asset folder. Multer and storage are fine. When i uploaded Image to Second server directly its working fine.
The first thing you need to do is create an API using node/Express.js and create store using multer:
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'uploads/');
},
// By default, multer removes file extensions so let's add them back
filename: function(req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() +
path.extname(file.originalname));
}
});
Build the image filter function:
const imageFilter = function(req, file, cb) {
// Accept images only
if (!file.originalname.match(/\.(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF)$/)) {
req.fileValidationError = 'Only image files are allowed!';
return cb(new Error('Only image files are allowed!'), false);
}
cb(null, true);
};
exports.imageFilter = imageFilter;
Create an API to handle image get from request:
app.post('/upload-pic', (req, res) => {
let upload = multer({ storage: storage, fileFilter: helpers.imageFilter }).single('pic');
upload(req, res, function(err) {
// req.file contains information of uploaded file
// req.body contains information of text fields, if there were any
if (req.fileValidationError) {
return res.send(req.fileValidationError);
}
else if (!req.file) {
return res.send('Please select an image to upload');
}
else if (err instanceof multer.MulterError) {
return res.send(err);
}
else if (err) {
return res.send(err);
}
// Display uploaded image for user validation
res.send(`You have uploaded this image`);
});
});
Now you have the server side accept the image from request and save it on file. After that, let us go back to the other server. On other server it's like a client and we need create request to the API upload-pic . To do that you can use axios package and form-data package.
Handling File Uploads
I have my back-end server in express(node.js) and all apis is running on this server. I also have file-upload mechanism for file-upload api using multer. For file uploading i have created a middleware and in my helper controller i have this
const storage = multer.diskStorage({
destination: (req, file, cb) => {
let path = getFileStoragePath(req, file);
console.log(`path to create ${path}`)
// let path = `uploads/transId${req.body.refrenceId}/transporter`
checkDirectory(path, (err) => {
if (err) {
console.log(`Error occured if checkDirectory ${err.message}`)
cb(err, null)
} else {
cb(null, path);
}
});
},
filename: (req, file, cb) => {
let dateNow = new Date()
cb(null, `${file.fieldname}_${dateformat(dateNow, 'dddd_mmmm_dS_yyyy_h_MM_ss_TT')}${path.extname(file.originalname)}`)
}
});
const saveFilesToFolder = async(req, res, next) => {
const upload = multer({
storage: storage,
fileFilter: imageFilter,
limits: {
fileSize: 1024 * 1024 * 10
}
}).any();
upload(req, res, (err) => {
const wasValidRequest = checkAllowedFiles(req);
if (wasValidRequest.status === false) {
return res.send({
status: false,
message: wasValidRequest.message,
response: null
})
}
// counter = 0
if (err) {
console.log(`Error uploading files, ${err.message}`)
return res.send({
status: false,
message: `Error occurred while uploading files, ${err.message}`,
response: null
})
}
// WHEN FILE UPLOADING IS DONE NOW PASSING THE REQUEST
next();
});
};
And in my route.js file i have attached my middleware to save files into folder and reading files like this
router.post('/upload-files', saveFilesToFolder, catchAsyncErrors(fileController.UploadFiles));
but now my requirement is that i want to read the content of file which is coming in incoming requestwithout saving that file to local disk by accessing the file inside my fileController function and i want to make a separate api for this purpose?
How can i do this
Multer provide memory options by which without storing file in local system, we can convert it into buffer and read the content.
Refer this or this
var storage = multer.memoryStorage();
var upload = multer({ storage: storage });
app.post('/imagenes', upload.single('image_field'), function(req,res){
req.file.buffer;
});
In controller you can use
console.log(String(req.file.buffer))
to look into content
My requirement was to use multer milddleware inside of my controller function to access/read incoming file from request. So i have achieved that and i am giving an answer for future readers so i did like this
controller.js
var storage = multer.memoryStorage({
destination: function(req, file, callback) {
callback(null, '');
}
});
var upload = multer({ storage: storage }).any();
const saveFilesToS3 = async(req, res) => {
upload(req, res, async(err) => {
console.log(req.files[0].buffer) // printing incoming file content as buffer
// rest of the code here
})
}
I am using express server and multer for upload file on different services (local, azure, cloudinary, amazon s3 etc).
For that i am using different module of multer multer-azure, multer-cloudinary etc.
I need this configuration will be applied to user wise and that information comes from the database.
So i need a extra call to fetch data from database before multer come in action.
I am able to call database query but when i am trying to call multer function, req parameter coming blank. Here is what i am doing.
var multerUtility = require('./upload/multer.utility');
let multer = new multerUtility().getActiveMulterService();
router.post('/', getMetadataConfiguration, multer, (req, res, next) => {
console.log('========== req ==========', req.file); // It is coming blank
console.log('========== req ==========', req.body); // It is coming blank
});
Here is first middleware function, which fetch data from database to verifiy which service will use to upload file.
function getMetadataConfiguration(res, req, next) {
var conn = new jsforce.Connection({
loginUrl : config.org_url,
});
var records = [];
conn.login(username, password, function(err, userInfo) {
if (err) {
return console.error(err);
}
conn.query("query", (err, result) => {
if(err) {
res.status(500).send(err);
}
console.log('=========== result=========', result);
req.serviceConfig = result.records[0];
next();
});
});
}
And here is my MulterUtility Class to handle configuration:
upload/multer.utility.js
class MulterUtility {
constructor() {
}
getActiveMulterService(req, res, next) {
var multerConfiguration;
if(req.serviceConfig.service == 'azure') {
multerConfiguration = multer({
storage: multerAzure({
connectionString: config.azure.connectionString,
account: config.azure.account,
key: config.azure.key,
container: config.azure.container
})
}).single('image');
} else if(req.serviceConfig.service == 'cloudinary') {
multerConfiguration = multer({
storage: cloudinaryStorage({
cloudinary: cloudinary,
folder: config.storageFolder
// allowedFormats: ['jpg', 'png', 'jpeg']
})
}).single('image');
} else if(req.serviceConfig.service === 'amazon') {
multerConfiguration = multer({
storage: multerS3({
s3: s3,
bucket: 'mycontainer',
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
metadata: function (req, file, cb) {
cb(null, {fieldName: file.fieldname});
},
key: function (req, file, cb) {
cb(null, Date.now().toString() + '-' + file.originalname)
}
})
}).single('image');
} else if(req.serviceConfig.service === 'local') {
multerConfiguration = multer({
storage: multer.memoryStorage()
}).single('image');
}
return multerConfiguration;
}
}
module.exports = MulterUtility;
After executing multer, i am not recieving a req.file or req.body params what multer sets after uploading file.
For now you can consider the 'local' file upload as mentioned in last condition.
The problem is that you call the method getActiveMulterService once before the router. But you need to call it for each post. Try something like this:
var multerUtility = require('./upload/multer.utility');
let multers = new multerUtility();
router.post('/',
getMetadataConfiguration,
(req, res, next) => multers.getActiveMulterService(req, res, next)(req, res, next),
(req, res, next) => {
console.log('========== req ==========', req.file); // It is coming blank
console.log('========== req ==========', req.body); // It is coming blank
});
And in this function you have arguments in the wrong order:
getMetadataConfiguration(res, req, next)
// ==>
getMetadataConfiguration(req, res, next)
Hi this has largely been answered already. The solution is to manual add your file object back onto your req.body object during the process.
Full solution is found here
The moment I try to upload a file with heroku to the database it won't work.
If I do it with localhost it does work.
Anybody has an idea why heroku is not handling my file when I try to upload a file to it? Does someone know a fix? I am kind of new to Heroku, and since I get no error in the logs I don't know what is going wrong.
The following is my code:
function addVideo(req, res) {
console.log(req.files);
User.findOne({ 'userName' : req.params.username }, function (err, user) {
var video = new Video();
video.sporter = user;
video.save()
.then(video => {
vid = video;
console.log(vid);
upload(req, res, function(err) {
if (err)
handleError(req, res, 500, err);
});
res.status(201).json(video);
})
.fail(err => handleError(req, res, 500, err));
});
}
var storage = GridFsStorage({
gfs : gfs,
chunkSize: 32740 ,
filename: function (req, file, cb) {
console.log(file);
cb(null, vid._id);
},
/** With gridfs we can store additional meta-data along with the file */
metadata: function(req, file, cb) {
console.log(vid);
cb(null,
{ originalname: file.originalname,
videoId: vid._id
});
},
root: 'ctFiles' //root name for collection to store files into
});
var upload = multer({ //multer settings for single upload
storage: storage
}).single('file');
As u see in my code, I create a new video, which I link to through my metadata. It does create a new Video model, but it doesn't create a new ctFiles.files object.
I'm currently implementing a file/image upload service for my users. I want to transform these images (resize/optimize) before uploading to my s3 bucket.
What I'm currently doing: Using a multipart form on my frontend (I think the actual implementation doesn't matter here..) and the multer and multer-s3 packages on my backend.
Here my implementation stripped down to the important parts.
// SETUP
var multer = require('multer');
var s3 = require('multer-s3');
var storage = s3({
dirname: 'user/uploads',
bucket: auth.aws.s3.bucket,
secretAccessKey: auth.aws.s3.secretAccessKey,
accessKeyId: auth.aws.s3.accessKeyId,
region: auth.aws.s3.region,
filename: function (req, file, cb) {
cb(null, Date.now());
}
});
var upload = multer({storage: storage}).single('img');
// ROUTE
module.exports = Router()
.post('/', function (req, res, next) {
upload(req, res, function (err) {
if (err) {
return res.status(401).json({err: '...'});
}
return res.json({err:null,url: '..'});
});
});
What I want to do: transform the image before uploading it. I'm not sure if I need to use multer/busboy here or I can just do it with NodeJS (thus I've tagged NodeJS and express as well).
So my question is: where can I intercept the upload and transform it before uploading it to my S3 bucket?
Not sure if you're still looking for an answer to this, but I had the same problem. I decided to extend the multer-s3 package.
I've opened a pull request to the original repository, but for now, you can use my fork.
Here's an example of how to use the extended version:
var upload = multer({
storage: multerS3({
s3: s3,
bucket: 'some-bucket',
shouldTransform: function (req, file, cb) {
cb(null, /^image/i.test(file.mimetype))
},
transforms: [{
id: 'original',
key: function (req, file, cb) {
cb(null, 'image-original.jpg')
},
transform: function (req, file, cb) {
cb(null, sharp().jpg())
}
}, {
id: 'thumbnail',
key: function (req, file, cb) {
cb(null, 'image-thumbnail.jpg')
},
transform: function (req, file, cb) {
cb(null, sharp().resize(100, 100).jpg())
}
}]
})
})
EDIT: My fork is also now available via npm under the name multer-s3-transform.
I've tried using #ItsGreg's fork, but couldn't get it to work. I managed to get this behaviour working by using multer-s3 standard configuration, and inside my file upload endpoint, i.e.,
app.post('/files/upload', upload.single('file'), (req, res) => {...})
I am retrieving the file using request, and passing the Buffer to sharp. The following works (and assumes you are using ~/.aws/credentials):
let request = require('request').defaults({ encoding: null });
let dataURI = `https://s3.amazonaws.com/${process.env.AWS_S3_BUCKET}/${image.defaultUrl}`;
request.get(dataURI, function (error, response, body) {
if (! error && response.statusCode === 200) {
let buffer = new Buffer(body);
const sizes = ['thumbnail', 'medium', 'large'];
sizes.forEach(size => {
sharp(buffer)
.resize(image.sizes[size])
.toBuffer()
.then(data => {
// Upload the resized image Buffer to AWS S3.
let params = {
Body: data,
Bucket: process.env.AWS_S3_BUCKET,
Key: `${image.filePath}${image.names[size]}`,
ServerSideEncryption: "AES256",
};
s3.putObject(params, (err, data) => {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
})
})
}
});