简体   繁体   中英

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

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.

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.

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
  
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM