1

I'm trying to pass a file (image) from my Reactjs app to Firebase Functions to then upload it to Pinata using their Node.js sdk but my cloud function keeps returning: ERR_INVALID_ARG_TYPE

Is it possible to pass a file to Firebase Functions?

I need to eventually convert it to a readable stream to use Pinata.

Image file: enter image description here

cormacncheese
  • 1,251
  • 1
  • 13
  • 27

2 Answers2

5

Callable Cloud Functions only accept JSON types, so you can't pass a File object to them.

What you can do is:

  • either read the bytes from the file in your client, and then pass the byte[] (which is a JSON type) or pass it as a base64 encoded String.
  • or you can write the file to Cloud Storage through Firebase, and then pass the path to that file to your Cloud Function
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

Used Frank van Puffelen's advice and first uploaded the file to Firebase Storage, then downloaded it in my Firebase Function, then converted the file to an array buffer.

Download from Google Cloud Storage

const [bufferFile] = await admin
     .storage()
     .bucket('my-bucket-name')
     .file('file-path)
     .download()

Convert to Array Buffer

const toArrayBuffer = (buf: Buffer) => {
     const ab = new ArrayBuffer(buf.length)
     const view = new Uint8Array(ab)
     for (let i = 0; i < buf.length; ++i) {
       view[i] = buf[i]
     }
     return ab
}

Then you can pass the Array Buffer to IPFS or arweave

cormacncheese
  • 1,251
  • 1
  • 13
  • 27