Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read from Firebase/Google Cloud Storage as a Buffer in Node.js function

I am using Node.js 8 version of Firebase functions and would like to retrieve a file from Google Cloud Storage to the function as a Buffer.

This seems straightforward:

const admin = require('firebase-admin')
const getRawBody = require('raw-body')

async function myFunction (path) {
  const bucket = admin.storage().bucket()
  const file = await bucket.file(path).get()
  console.log("All good so far.")
  const buffer = await getRawBody(file.createReadStream()) // often fails.
}

The file I'm reading is trivially small (130kb).

This did work for a while, but now consistently fails with either a memory limit error or a timeout error, suggesting a Firebase/GCS issue.

I am aware I could increase the memory limit for the function, but if it failing on a trivially small file suggests grander problems at work and that it'll invariably fail on larger files.

Is there a better way to convert a GCS file to a Buffer?

If not, is the issue one that can be worked around?

like image 921
Brian M. Hunt Avatar asked Jun 01 '26 08:06

Brian M. Hunt


1 Answers

Worked for me when I removed get() from const file = await bucket.file(path).get()

My working code is:

const admin = require('firebase-admin')
const getRawBody = require('raw-body')

async function getStream (path) {
    const bucket = admin.storage().bucket()
    const file = await bucket.file(path)
    const buffer = await getRawBody(file.createReadStream()) 
}
like image 165
cormacncheese Avatar answered Jun 03 '26 07:06

cormacncheese



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!