Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// middleware.js
exports.filesUpload = function(req, res, next) {
// See https://cloud.google.com/functions/docs/writing/http#multipart_data
const busboy = new Busboy({
headers: req.headers,
limits: {
// Cloud functions impose this restriction anyway
fileSize: 10 * 1024 * 1024,
}
});
const fields = {};
const files = [];
const fileWrites = [];
// Note: os.tmpdir() points to an in-memory file system on GCF
// Thus, any files in it must fit in the instance's memory.
const tmpdir = os.tmpdir();
busboy.on('field', (key, value) => {
// You could do additional deserialization logic here, values will just be
// strings
fields[key] = value;
});
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
const filepath = path.join(tmpdir, filename);
console.log(`Handling file upload field ${fieldname}: ${filename} (${filepath})`);
const writeStream = fs.createWriteStream(filepath);
file.pipe(writeStream);
fileWrites.push(new Promise((resolve, reject) => {
file.on('end', () => writeStream.end());
writeStream.on('finish', () => {
fs.readFile(filepath, (err, buffer) => {
const size = Buffer.byteLength(buffer);
console.log(`${filename} is ${size} bytes`);
if (err) {
return reject(err);
}
files.push({
fieldname,
originalname: filename,
encoding,
mimetype,
buffer,
size,
});
try {
fs.unlinkSync(filepath);
} catch (error) {
return reject(error);
}
resolve();
});
});
writeStream.on('error', reject);
}));
});
busboy.on('finish', () => {
Promise.all(fileWrites)
.then(() => {
req.body = fields;
req.files = files;
next();
})
.catch(next);
});
busboy.end(req.rawBody);
}
@191611007

This comment has been minimized.

Copy link

191611007 commented Jan 15, 2020

I used this to upload image into firebase but it didn't work

@msukmanowsky

This comment has been minimized.

Copy link
Owner Author

msukmanowsky commented Jan 15, 2020

@191611007 sorry to hear that, what was the error you got?

@191611007

This comment has been minimized.

Copy link

191611007 commented Jan 17, 2020

I tried to upload an image. I selected a file in postman, it's just got executed but there is no error and no image in firbase storage

@msukmanowsky

This comment has been minimized.

Copy link
Owner Author

msukmanowsky commented Jan 18, 2020

Hmm it's tough to debug that on my end without some more details. Want to provide some sample code?

@191611007

This comment has been minimized.

Copy link

191611007 commented Jan 19, 2020

Error: Bucket name not specified or invalid. Specify a valid bucket name via the storageBucket option when initializing the app, or specify the bucket name explicitly when calling the getBucket() method.
at new FirebaseError (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28)
at Storage.bucket (/srv/node_modules/firebase-admin/lib/storage/storage.js:107:15)
at Busboy.busboy.on (/srv/handlers.js/users.js:108:25)
at emitNone (events.js:106:13)
at Busboy.emit (events.js:208:7)
at Busboy.emit (/srv/node_modules/busboy/lib/main.js:37:33)
at UrlEncoded.end (/srv/node_modules/busboy/lib/types/urlencoded.js:211:12)
at Busboy.emit (/srv/node_modules/busboy/lib/main.js:30:36)
at finishMaybe (_stream_writable.js:613:14)
at afterWrite (_stream_writable.js:464:3)

@191611007

This comment has been minimized.

Copy link

191611007 commented Jan 19, 2020

exports.uploadImage = (req, res) => {

const BusBoy = require('busboy');
const path = require('path');
const os = require('os');
const fs = require('fs');

const busboy = new BusBoy({headers: req.headers});
let imageFileName;
let imageTobeUploaded = {};
busboy.on('file',(fieldname, file, filename, encoding, mimeType) => {
    //my.image.png
    const imageExtension = filename.split('.')[filename.split('.').length - 1];
    //12345678900.png
    imageFileName = `${Math.round(Math.random()*100000000000)}.${imageExtension}`;
    const filepath = path.join(os.tmpdir(), imageFileName);
    imageTobeUploaded = {filepath, mimeType};
    file.pipe(fs.createWriteStream(filepath)); 
})
busboy.on('finish', () => {
    admin.storage().bucket().upload(imageTobeUploaded.filepath, {
        resumable: false,
        metadata:{
            metadata:{
                contentType:imageTobeUploaded.mimeType
            }
        }
    })
    .then( () => {
        const imageUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/${imageFileName}?alt=media`
       // return db.doc(`/users/${req.user.handle}`).update({imageUrl});
    })
    .then( () => {
        return res.json({message: "Image Uploaded Successfully"});
    })
    .catch(err => {
        console.error(err);
        return res.status(400).json({ error : err.code});
    })
});
busboy.end(req.rawBody);

};

@191611007

This comment has been minimized.

Copy link

191611007 commented Jan 19, 2020

I tried with different code
I got different error previously like below
SyntaxError: Unexpected token o in JSON at position 1

now I got that bucket invalid error

@191611007

This comment has been minimized.

Copy link

191611007 commented Jan 20, 2020

TypeError: Path must be a string. Received undefined
at assertPath (path.js:28:11)
at Object.basename (path.js:1377:5)
at Bucket.upload (/srv/node_modules/@google-cloud/storage/build/src/bucket.js:2345:38)
at PromiseCtor (/srv/node_modules/@google-cloud/promisify/build/src/index.js:71:28)
at new Promise ()
at Bucket.wrapper (/srv/node_modules/@google-cloud/promisify/build/src/index.js:56:16)
at Busboy.busboy.on (/srv/handlers.js/users.js:108:34)
at emitNone (events.js:106:13)
at Busboy.emit (events.js:208:7)
at Busboy.emit (/srv/node_modules/busboy/lib/main.js:37:33)

if I do firebase deploye then I am getting above path error
if I do firebase serve then I am getting bucket error

@191611007

This comment has been minimized.

Copy link

191611007 commented Jan 20, 2020

TypeError: Path must be a string. Received undefined
at assertPath (path.js:28:11)
at Object.basename (path.js:1377:5)
at Bucket.upload (/srv/node_modules/@google-cloud/storage/build/src/bucket.js:2345:38)
at PromiseCtor (/srv/node_modules/@google-cloud/promisify/build/src/index.js:71:28)
at new Promise ()
at Bucket.wrapper (/srv/node_modules/@google-cloud/promisify/build/src/index.js:56:16)
at Busboy.busboy.on (/srv/handlers.js/users.js:108:34)
at emitNone (events.js:106:13)
at Busboy.emit (events.js:208:7)
at Busboy.emit (/srv/node_modules/busboy/lib/main.js:37:33)
If I do firebase deploy I am getting above path error
if I do firebase serve then It will be bucket error

help me out this bro

@191611007

This comment has been minimized.

Copy link

191611007 commented Jan 21, 2020

I found the error

in admin.storage().bucket() there should give storage bucket as bucket(config.storageBucket)

@msukmanowsky

This comment has been minimized.

Copy link
Owner Author

msukmanowsky commented Jan 22, 2020

🎉 🎆 👍

@abhayverma47

This comment has been minimized.

Copy link

abhayverma47 commented Mar 9, 2020

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.