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

编程入门 行业动态 更新时间:2024-10-04 23:32:26

如何从云<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数中的云存储读取文件的内容?"/>

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

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

的实例
回答如下:

更多推荐

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

本文发布于:2024-05-30 10:42:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   文件   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!