简体   繁体   English

如何将文件传递给 Firebase 函数

[英]How to pass File to Firebase Functions

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我正在尝试将文件(图像)从我的 Reactjs 应用程序传递到 Firebase Functions,然后使用他们的 Node.js sdk 将其上传到Pinata ,但我的云函数不断返回: ERR_INVALID_ARG_TYPE

Is it possible to pass a file to Firebase Functions?是否可以将文件传递给 Firebase 函数?

I need to eventually convert it to a readable stream to use Pinata.我最终需要将其转换为可读流以使用 Pinata。

Image file:图像文件: 在此处输入图像描述

Callable Cloud Functions only accept JSON types, so you can't pass a File object to them. Callable Cloud Functions仅接受 JSON 类型,因此您不能将File对象传递给它们。

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.从客户端中的文件中读取字节,然后传递byte[] (这是一种 JSON 类型)或将其作为 base64 编码的字符串传递。
  • or you can write the file to Cloud Storage through Firebase , and then pass the path to that file to your Cloud Function或者您可以通过 Firebase 将文件写入 Cloud Storage ,然后将该文件的路径传递给您的 Cloud Function

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.使用 Frank van Puffelen 的建议,首先将文件上传到 Firebase 存储,然后将其下载到我的 Firebase 函数中,然后将文件转换为数组缓冲区。

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然后您可以将数组缓冲区传递给 IPFS 或 arweave

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Firebase函数将文件列表上传到Firebase存储 - How to upload a list of file to firebase storage from firebase functions 如何在 firebase 中排队函数 - How to enqueue functions in firebase Firebase Cloud Functions-如何获取上载文件的用户? - Firebase Cloud Functions - How to get user that uploads a file? 如何使用 firebase 函数从父文件夹读取文件 - How to read a file from parent folder using firebase functions 通过 Firebase 函数下载文件到 Firebase 存储 - Download file to Firebase Storage via Firebase Functions 如何传递Promise.all返回另一个函数Cloud Functions for Firebase - How to pass a Promise.all return to another function Cloud Functions for Firebase 如何使用 firebase 函数返回保存在 firebase 存储中的文件的 json 内容 - How to use firebase functions to return the json content of a file saved in firebase storage 上传到Firebase存储时如何使用Cloud Functions for FIrebase获取mp3文件的持续时间 - How to get the duration of a mp3 file using Cloud Functions for FIrebase when uploaded to firebase storage 如何使用 Firebase 函数在 Firebase 存储上上传一个 JSON 的新文件? - How do you upload a new file with a JSON on Firebase Storage using Firebase Functions? 如何正确地将函数传递给javascript中的其他函数 - How to properly pass functions to other functions in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM