Reputation: 437
I'm trying to upload files to google cloud bucket using node js and express-file upload. So far when I upload a file itself gets uploaded to the bucket but the file size doesn't, which makes the image not working.
// uploading single image
const fileData = req.files.files
const creteFeed = await new Feed({
feedby: req.user,
feedTxt: txt,
deleted: 0,
feedReaction: 0,
feedComment: 0,
feedShare: 0,
feedType: Feedtype,
})
const newFeed = await creteFeed.save()
if (newFeed) {
const FeedMed = await new FeedMedia({
feedby: req.user,
feed_id: newFeed._id,
feedMedia_type:fileData.mimetype,
feedMedia_name: fileData.name,
category:'feed',
url: `https://storage.googleapis.com/${bucket.name}/${fileData.name}`,
deleted: 0
})
const newFeedMed = await FeedMed.save()
if(newFeedMed){
// Create a new blob in the bucket and upload the file data.
const blob = bucket.file(fileData.name);
const blobStream = blob.createWriteStream();
blobStream.on('finish', res => {
});
blobStream.on('finish', () => {
// The public URL can be used to directly access the file via HTTP.
const publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`
res.status(200).send(publicUrl);
});
blobStream.end(fileData.buffer);
// res.json('success')
}
}
}
Upvotes: 1
Views: 579
Reputation: 437
well after searching for a while i just had to switch to using multer
const gc = new Storage({
keyFilename: Path.join(__dirname,process.env.gcBucket.jsonpath),
projectId:process.env.gcBucketID
})
const bucket = gc.bucket(process.env.gcBucketname)
// Multer is required to process file uploads and make them available via
// req.files.
const multer = Multer({
storage: Multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
},
});
Router.post('/pageFeed',multer.array('files'),authoriazation,async(req,res)=>{
const { txt,Feedtype,feedby } = req.body
// uploading multiple image
if(req.files.length > 0){
const creteFeed = await new Feed({
feedby: feedby,
feedTxt: txt,
deleted: 0,
feedReaction: 0,
feedComment: 0,
feedShare: 0,
feedType: Feedtype,
})
const newFeed = await creteFeed.save()
if(newFeed){
for (newfile of req.files) {
const FeedMed = await new FeedMedia({
feed_id: newFeed._id,
feedby: feedby,
feedMedia_type:newfile.mimetype,
feedMedia_name:newfile.originalname,
category:'feed',
url: `https://storage.googleapis.com/${bucket.name}/${newfile.originalname}`,
deleted:0
})
const newFeedMed = await FeedMed.save()
if(newFeedMed){
const blob = bucket.file(newfile.originalname);
const blobStream = blob.createWriteStream();
blobStream.on('finish', () => {
// The public URL can be used to directly access the file via HTTP.
res.json({msg:'success'})
});
blobStream.end(newfile.buffer);
}else{
res.json({error:'error uploading'})
}
}
}
}else{
const creteFeed = await new Feed({
feedby: feedby,
feedTxt: txt,
deleted: 0,
feedReaction: 0,
feedComment: 0,
feedShare: 0,
feedType: Feedtype,
})
const newFeed = await creteFeed.save()
if(newFeed){
res.json('success')
}
}
})
Upvotes: 1