函数中的云存储读取文件的内容?"/>
如何从云函数中的云存储读取文件的内容?
如何从 HTTP onCall 函数读取文本文件的内容?我的目标是将文件作为字符串读取,然后从字符串中提取所有电子邮件地址,然后返回生成的电子邮件数组。但是我在读取文件时遇到了问题。
请注意,我提前知道文件在云存储中的位置。 IE。我知道路径是
users/${context.auth.uid}/customers
这是我试过的
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const { getAuth } = require('firebase-admin/auth')
const { getStorage } = require('firebase-admin/storage')
const { readFile } = require('fs')
admin.initializeApp()
const db = admin.firestore()
const bucket = getStorage().bucket()
exports.readCSVFile = functions.https.onCall(async (data, context) => {
// Read text file and identify email addresses within it
// Return array of email addresses
const file = bucket.file(`users/${context.auth.uid}/customers`)
return file.exists().then(async (data) => {
if (data[0]) {
return readFile(file.createReadStream(), "utf8", function(err, contents) {
if (err) {
throw new functions.https.HttpsError(
'internal',
"Could not read customers file"
)
} else {
console.log("contents", contents)
return contents
}
})
} else {
throw new functions.https.HttpsError(
'not-found',
"customers file not found"
)
}
})
.catch((err) => {
console.error(`Error: ${err}`)
})
})
这给了我错误
回答如下:错误:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型或 Buffer 或 URL 的实例。接收到 PassThroughShim
的实例
更多推荐
如何从云函数中的云存储读取文件的内容?
发布评论