【问题标题】:How do you create a Google Cloud Function that returns a list of objects from Cloud Storage如何创建从 Cloud Storage 返回对象列表的 Google Cloud 函数
【发布时间】:2020-04-25 12:36:38
【问题描述】:

所以我试图返回一个对象数组。我想使用云功能列出谷歌云存储中特定存储桶中的所有对象。以下是我到目前为止所拥有的。它返回和未处理的承诺。

export const getAlbums = functions.https.onCall(() => {
  return new Promise(async (resolve, reject) => {
    //const bucket = admin.storage().bucket('kais-e4ba9.appspot.com')

    const { Storage } = require("@google-cloud/storage");

    const storage = new Storage();
    const bucket = storage.bucket("bucketName");

    resolve(bucket)
  }).catch(err => {
    console.log(err.message);
  });
});

我写了一个类似的函数,它从 firebase firestore 返回一个文档数组。

export const getGenres = functions.https.onCall(() => {
  //if (!context.auth) return {status: 'error', code: 401, message: 'Not signed in'}
  return new Promise((resolve, reject) => {
    const array = [{}];
    const ref = admin.firestore().collection("genres");
    // filter out inaActive genres
    const query = ref.where("isActive", "==", true);
    query.onSnapshot(querySnapshot => {
      // Add genres into an array
      const genres = querySnapshot.docs.map(documentSnapshot => {
        return {
          ...documentSnapshot.data(),
          key: documentSnapshot.id // required for FlatList
        };
      });
      array.push(genres);
      resolve(array);
    });
  });
});

这是您将其记录到控制台时返回的内容。当我调用云函数列出存储桶中的对象时,我想显示相同的结果。

https://firebasestorage.googleapis.com/v0/b/kais-e4ba9.appspot.com/o/Screen%20Shot%202020-01-07%20at%207.22.27%20PM.png?alt=media&token=b777d63d-3053-4453-86e7-1e2f19f97a51

【问题讨论】:

    标签: typescript firebase async-await google-cloud-functions google-cloud-storage


    【解决方案1】:

    使用 GCP 的 Cloud Functions 和 Node.JS,您可以使用 getFiles() 方法将文件从 Cloud Storage 存储桶检索到列表中,然后按此处演示的那样遍历它们:

      // Imports the Google Cloud client library
      const {Storage} = require('@google-cloud/storage');
    
      // Creates a client
      const storage = new Storage();
    
      /**
       * TODO(developer): Uncomment the following line before running the sample.
       */
      // const bucketName = 'Name of a bucket, e.g. my-bucket';
    
      // Lists files in the bucket
      const [files] = await storage.bucket(bucketName).getFiles();
    
      console.log('Files:');
      files.forEach(file => {
        console.log(file.name);
      });
    

    【讨论】:

      【解决方案2】:

      您将希望使用存储桶中的Cloud Storage list API to 列表文件。它与 Firestore 100% 无关。

      这是从链接文档中提取的代码:

      const [files] = await storage.bucket(bucketName).getFiles();
      
      console.log('Files:');
      files.forEach(file => {
        console.log(file.name);
      });
      

      将它添加到您的函数中很简单。你从“新承诺”开始的方式并不是最好的方式。我们将使用 async/await 语法以使示例更易于复制:

      export const getAlbums = functions.https.onCall(async () => {
          const { Storage } = require("@google-cloud/storage");
      
          const storage = new Storage();
          const [files] = await storage.bucket(bucketName).getFiles();
      
          console.log('Files:', files);
          return files.map(file => file.name)
      });
      

      如果您只想要项目的默认存储桶,您可以完全省略 bucketName 参数。

      【讨论】:

      • 这样实际上会返回存储桶中超出文件夹级别的所有文件。有没有办法只返回目录中的文件夹而不是所有文件?
      • 是的,列表 API 有一种按前缀过滤的方法。阅读 API 文档以了解如何使用它。 googleapis.dev/nodejs/storage/latest/Bucket.html#getFiles
      猜你喜欢
      • 1970-01-01
      • 2019-04-15
      • 2017-02-08
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多