简体   繁体   English

如何做云 function firebase 将数据从 Bucket 实时导入数据库

[英]how to do a cloud function firebase importing data from Bucket to database realtime

I tried with this code but it's not working, I need do a cloud function firebase importing data from Bucket to database realtime我尝试使用此代码,但它不起作用,我需要做一个云 function firebase 将数据从存储桶实时导入数据库

exports.getbackups = functions.runWith({ memory: "128MB", timeoutSeconds: 60, }).https.onRequest(async (req, res) => {
      res = FirebaseServices.setHeaders(res);
      let send = await FirebaseServices.verifyIdToken(req);
      var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    
        const url = 'https://storage.cloud.google.com/report-transfer-data/2022-08-01T11%3A12%3A57Z_sp200200011002-report_data.json.gz?authuser=1'
        const xhr = new XMLHttpRequest();
        xhr.responseType = 'blob';
        xhr.onload = (event) => {
          const blob = xhr.response;
        };
        xhr.open('GET', url);
        xhr.send();
    
        const baseReport = document.getElementById('baseReport');
        baseReport.setAttribute('src', url);
    
    });

From your Cloud Function you need to interact with the Cloud Storage service via the Admin SDK.在您的云 Function 中,您需要通过管理员 SDK 与云存储服务进行交互。

In particular, you'll find in the SDK Reference an example on how to download the file into CF memory and then you can use its content to write to the RTDB.特别是,您会在SDK 参考中找到有关如何将文件下载到 CF memory的示例,然后您可以使用其内容写入 RTDB。

Something along the following lines:大致如下:

exports.getbackups = functions.runWith({ memory: "128MB", timeoutSeconds: 60, }).https.onRequest(async (req, res) => {
    
    const filePath = ...;  // file path in Cloud Storage without the gs://
    
    const file = admin
    .storage()
    .bucket()
    .file(filepath);
    
    const downloadResponse = await file.download();
    const contents = downloadResponse[0];
    
    // Do what you want with contents
    
    res.send(....);  // Terminate the HTTPS Cloud Function
    // Correctly terminating your CF is important. See https://firebase.google.com/docs/functions/terminate-functions
    // and in particular the embedded videos
  
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从云功能中止到Firebase实时数据库的创建? - How to abort creation into firebase realtime database from a cloud function? Firebase实时数据库中的云功能 - Cloud Function in Firebase Realtime Database 如何在使用 Cloud Functions 时使用 Firebase Admin SDK 更改 Firebase 实时数据库中的数据? - How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions? 如何从 web 中的 firebase 实时数据库获取数据? - How do I get data from firebase realtime database in web? 如何使用 firebase 云函数从 firebase-realtime-database 中获取数据? - How to fetch data from firebase-realtime-database using firebase cloud functions? 如何使用云功能将子级添加到Firebase实时数据库的列表中? - How to add child to a List in Firebase Realtime Database with a cloud function? 如何使用云 function 获取 Firebase 中的实时数据库? - How to get Realtime Database in Firebase by using cloud function? 如何从Firebase实时数据库中检索数据? - How to retrieve data from firebase realtime database? 如何使用云功能将新数据推送到Firebase实时数据库中? - How to push new data into firebase realtime database using cloud functions? 如何使用 Javascript 云从 Firebase 实时数据库中随机获取子密钥名称 function - How to get a child key name randomly from the Firebase Realtime database with a Javascript cloud function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM