[英]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。
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:你可以做的是:
byte[]
(which is a JSON type) or pass it as a base64 encoded String.从客户端中的文件中读取字节,然后传递byte[]
(这是一种 JSON 类型)或将其作为 base64 编码的字符串传递。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.