加上火基云函數以及Firebase與GoogleCloud的更深層次的集成,這已經成為可能。
使用云函數,您可以使用GoogleCloudNode包來完成史詩云存儲操作。下面是一個從CloudStorage獲取所有文件URL到數組中的示例。這個功能每次被保存到Google云存儲時都會被觸發。
附注1這是一個計算上相當昂貴的操作,因為它必須遍歷桶/文件夾中的所有文件。
附注2:我寫這個只是作為一個例子,沒有把很多細節寫成承諾等等。只是想給個主意。
const functions = require('firebase-functions');
const gcs = require('@google-cloud/storage')();
// let's trigger this function with a file upload to google cloud storage
exports.fileUploaded = functions.storage.object().onChange(event => {
const object = event.data; // the object that was just uploaded
const bucket = gcs.bucket(object.bucket);
const signedUrlConfig = { action: 'read', expires: '03-17-2025' }; // this is a signed url configuration object
var fileURLs = []; // array to hold all file urls
// this is just for the sake of this example. Ideally you should get the path from the object that is uploaded :)
const folderPath = "a/path/you/want/its/folder/size/calculated";
bucket.getFiles({ prefix: folderPath }, function(err, files) {
// files = array of file objects
// not the contents of these files, we're not downloading the files.
files.forEach(function(file) {
file.getSignedUrl(signedUrlConfig, function(err, fileURL) {
console.log(fileURL);
fileURLs.push(fileURL);
});
});
});
});
我希望這能給你一個大致的想法。要獲得更好的云功能示例,請查看Google的Gizubrepo充滿了用于FireBase的云功能示例..也檢查他們的GoogleCloudNodeAPI文檔