如何安全地將 Firestore 文檔與存儲(chǔ)文件連接起來
所以我需要將文檔保存到我的 Firestore,每個(gè)文檔都有一個(gè)文件鏈接(下載鏈接)。為了實(shí)現(xiàn)這一目標(biāo),我:將文件上傳到 Firebase 存儲(chǔ)上傳成功后獲取文件的下載鏈接。上傳帶有鏈接到存儲(chǔ)文件的 download_url 字段的文檔。您可以在代碼中看到這一點(diǎn)。這有以下問題:我的 javascript 都是客戶端(我使用 Github Pages),所以有人可以簡(jiǎn)單地更改下載鏈接并將其放入文檔上傳中。我可以添加 firestore 規(guī)則來檢查下載鏈接是否以https://firebasestorage.googleapis.com/v0/b/my_application.appspot.com/o/info開頭,但這真的是最好的方法嗎?如果上傳的文件合法,但文件不合法,文件將保存到我的存儲(chǔ)中,但文件將被拒絕。沒有參考該文件,除非我手動(dòng)將其刪除,否則它將始終保留在我的存儲(chǔ)中。function uploadFile(my_file) { // Create a root reference var storageRef = firebase.storage().ref(); var uploadTask = storageRef.child('info/' + my_file.name).put(my_file); uploadTask.on('state_changed', function (snapshot) { }, function (error) { console.error("Error uploading file: ", error); }, function () { // Handle successful uploads on complete uploadTask.snapshot.ref.getDownloadURL().then(function (downloadURL) { uploadDocument(downloadURL); }); });}function uploadDocument(downloadUrl) { var name = document.forms["infoForm"]["name"].value; var author = document.forms["infoForm"]["author"].value; var documentObject = { name: name, author: author, download_url: downloadUrl }; db.collection("info").add(documentObject) .then(function (docRef) { console.log("Document written with ID: ", docRef.id); }) .catch(function (error) { console.error("Error adding document: ", error); });}我不知道處理這個(gè)問題的最佳方法是什么。任何幫助將不勝感激:)
查看完整描述