Shamoon
Shamoon

Reputation: 43579

How can I pipe a file using multer to Google Cloud Storage?

I'm doing:

    const fileId = uuidv4()
    console.log(process.env.GOOGLE_PRIVATE_KEY)
    const storage = new GoogleCloudStorage({
        projectId: process.env.GOOGLE_RAW_AUDIO_BUCKET,
        credentials: {
            client_email: process.env.GOOGLE_CLIENT_EMAIL,
            private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n')
        }
    });

    const bucket = storage.bucket(process.env.GOOGLE_RAW_AUDIO_BUCKET);
    const blob = bucket.file(fileId + 'mp3');
    const blobStream = blob.createWriteStream()

    console.log(req.file)

    // var readableStream = fs.createReadStream(req.file.path);
    fs.createReadStream(req.file.buffer).pipe(blobStream)

But I get an error:

TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received <Buffer ff f3 38 c4 00 00 00 03 48 00 00 00 00 4c 41 4d 45 33 2e 39 39 2e 35 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 ..

Using multer and express. But I can't figure out how to stream from multer to GCS. Any tips would be greatly appreciated?

Upvotes: 0

Views: 619

Answers (1)

Shamoon
Shamoon

Reputation: 43579


    const storage = new GoogleCloudStorage({
        projectId: process.env.GOOGLE_RAW_AUDIO_BUCKET,
        credentials: {
            client_email: process.env.GOOGLE_CLIENT_EMAIL,
            private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n')
        }
    });

    const bucket = storage.bucket(process.env.GOOGLE_RAW_AUDIO_BUCKET);
    const blob = bucket.file(filePath);
    const blobStream = blob.createWriteStream()

    return new Promise((resolve, reject) => {
            streamifier.createReadStream(req.file.buffer)
                .on('error', (err) => {
                    return reject(err);
                })
                .pipe(blobStream)
                .on('finish', (resp) => {
                    return resolve();
                });
        })
        .then(() => {
            filePath = process.env.GOOGLE_RAW_AUDIO_BUCKET + '/' + filePath
            return db.models.Audio.create({
                filePath
            })
        })
        .then((dbAudio) => {
            console.log(dbAudio)
            let id = dbAudio.id
            res.send({ id, filePath })
        })

That did it!

Upvotes: 2

Related Questions