I have an Express route that connects to my google-storage bucket and uploads an image. I've tried to stick close to Googles code samples, but go from multer buffer straight to the bucket.
The process appears to complete without returning en err. However, the file appears in the bucket with 0B size. So it seems the meta data arrives but the image buffer does not...??
Can anyone see what I'm doing wrong?
If possible, I'd like to get this to work without the aid of additional NPM help packages like multer-cloud-storage and all the other variants.
const Multer = require("multer");
const multer = Multer({
storage: Multer.MemoryStorage,
fileSize: 5 * 1024 * 1024
});
const {Storage} = require('@google-cloud/storage');
const storage = new Storage({
projectId: 'project-name',
keyFile: '../config/project-name.json'
});
const bucket = storage.bucket('bucket-name');
app.post('/upload', multer.single('file'), function (req, res) {
const blob = bucket.file(req.file.originalname);
const blobStream = blob.createWriteStream({
metadata: {
contentType: req.file.mimetype
},
resumable: false
});
blobStream.on('error', err => {
next(err);
console.log(err);
return;
});
blobStream.on('finish', () => {
blob.makePublic().then(() => {
res.status(200).send(`Success!`);
})
})
blobStream.end();
})
