簡體   English   中英

如何做雲 function firebase 將數據從 Bucket 實時導入數據庫

[英]how to 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);
    
    });

在您的雲 Function 中,您需要通過管理員 SDK 與雲存儲服務進行交互。

特別是,您會在SDK 參考中找到有關如何將文件下載到 CF memory的示例,然后您可以使用其內容寫入 RTDB。

大致如下:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM