我尝试了这段代码,但是它不起作用,我需要做一个云函数防火墙,实时地将数据从Bucket导入到数据库
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);
});
发布于 2022-08-02 04:42:25
从云函数中,您需要通过Admin与云存储服务交互。
特别是,您将在SDK参考中找到一个如何编写将文件下载到CF内存中的示例,然后可以使用它的内容向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
});
https://stackoverflow.com/questions/73207231
复制