【问题标题】:how to stream data from cloud functions to cloud storage如何将数据从云功能流式传输到云存储
【发布时间】:2020-05-17 23:15:35
【问题描述】:

我正在尝试创建一个云功能,它将一些数据(来自 firestore 的文档)保存到云存储中。

之前写过一些云函数,但对云存储、存储桶等来说有点新。 根据我的阅读,我必须将这些数据“流式传输”到存储桶中。

我很想看到一个简短的 sn-p 做到这一点:)

【问题讨论】:

    标签: google-cloud-firestore cloud


    【解决方案1】:

    要做到这一点,应该不是很复杂,希望能帮到你。

    要执行此操作,我将按照文章 Backup Firestore data to storage bucket on a schedule in GCP 中解释的示例进行操作 - 如果您有兴趣,可以完全按照该示例进行操作 - 重点关注从 Firestore 到 Cloud Storage 的上传。我将解释使用哪些部分以及如何使用它们,以实现您的目标

    一旦您创建了 Cloud Storage 存储分区 - 它应该在设置中配置了 Multi-regionalNearline - 您需要使用它们后面指示的以下代码。

    index.js文件:

    const firestore = require('@google-cloud/firestore');
    const client = new firestore.v1.FirestoreAdminClient();
    // Replace BUCKET_NAME
    const bucket = 'gs://<bucket-id>'
    exports.scheduledFirestoreBackup = (event, context) => {
      const databaseName = client.databasePath(
        process.env.GCLOUD_PROJECT,
        '(default)'
      );
    return client
        .exportDocuments({
          name: databaseName,
          outputUriPrefix: bucket,
          // Leave collectionIds empty to export all collections
          // or define a list of collection IDs:
          // collectionIds: ['users', 'posts']
          collectionIds: [],
        })
        .then(responses => {
          const response = responses[0];
          console.log(`Operation Name: ${response['name']}`);
          return response;
        })
        .catch(err => {
          console.error(err);
        });
    };
    

    package.json文件:

    {
      "dependencies": {
        "@google-cloud/firestore": "^1.3.0"
      }
    }
    

    这些文件应该被创建,在云功能中的配置如下:一个唯一的名字; Cloud Sub/Pub 作为触发器;主题名称类似或等于initiateFirestoreBackup;使用Node.js,源码就是上面写的文件和函数来执行scheduledFirestoreBackup

    以上代码应该足以让您从 Firestore 导出到 Cloud Storage,因为它将获取您的所有集合 - 或者您可以定义细节 - 并发送到您已经创建的存储桶。

    此外,如果您想了解有关使用 Cloud Functions 将文件上传到 Cloud Storage 的更多信息,也可以在此处查看:Uploading files from Firebase Cloud Functions to Cloud Storage

    如果这些信息对您有帮助,请告诉我!

    【讨论】:

    • 是的,这个解决方案也可以在这里找到:firebase.google.com/docs/firestore/solutions/… 不幸的是,它导出的数据比我想要的多(子集合等),这意味着成本很高。使用您的第二个链接,我能够实现部分备份 - 这非常适合我的需求。惊人的!非常感谢:)
    • 当然,我理解你的观点,@user6097845,有道理。很高兴它对您有所帮助!
    【解决方案2】:

    感谢@gso_gabriel,我能够为我的文档创建部分备份。 对于任何感兴趣的人,这是我的代码的简化版本:

    const bucketName = 'myproject.appspot.com';
    
    exports.backup = functions.https.onCall(async (data, context) => {
       const userDoc = await admin.firestore().collection('users').doc('abc123').get();
       await writeFile('temp', 'user.json', JSON.stringify(userDoc.data()));
    }
    
    async function writeFile(dirName, fileName, content) {
       var bucket = admin.storage().bucket(bucketName);
       const destFilename = dirName + '/' + fileName;
       const file = bucket.file(destFilename);
       const options = {
          destination: destFilename,
          metadata: { contentType: "application/json" }
       };
       await bucket.file(destFilename).save(content, options);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 2020-11-23
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 2020-04-25
      • 2020-08-26
      • 2017-12-21
      相关资源
      最近更新 更多