skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search