最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何从云函数中的云存储读取文件的内容?

网站源码admin31浏览0评论

如何从云函数中的云存储读取文件的内容?

如何从云函数中的云存储读取文件的内容?

如何从 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

的实例
回答如下:
发布评论

评论列表(0)

  1. 暂无评论