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?
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())
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With