我有一个使用pub / sub从firebase删除文档的功能,但是,在删除文档之前(使用文档引用),我想获取保存在该文档字段中的链接的存储引用。 / p>
为了简化起见,我将举例说明,其中有约瑟夫(Joseph)文档具有以下字段:
username: "Joseph"; SexUser: "Male";
和urlProfileUser: "any-valid-link-to-download-to-storage-image-uploaded"
。
在删除文档之前,我要从所选文档中获取该字段,请通过链接获取引用(在Java中,我使用storage.getReferenceFromUrl (urlProfileUser)
),然后通过该链接从存储中删除该照片,以便,就是这样,我从Firestore中删除了文档。
删除我想要的文档的云函数的代码: (我现在只需要删除存储链接引用的图像...)
import * as functions from 'firebase-functions'
import * as admin from "firebase-admin";
admin.initializeApp();
//Scheduled job executed every day 23:00
exports.removeUsersUnavailable = functions.pubsub.schedule('0 23 * * *').onRun((context)=>
{
const db = admin.firestore();
const dateEvent = Date.now();
const cutOff = dateEvent - 24*60*60*1000; // After 24 hours(one day) delete document
db.collection("userManagers").orderBy('dateCreated').endAt(cutOff)
.get()
.then(snapshot => {
if(snapshot.empty){
console.log('Nothing still expirated');
return;
}
snapshot.forEach(doc =>{
//console.log(doc.id, "=>", doc.data);
console.log('Expirated, date deleted');
// Here I should delete the photo from the storage, since I already have the document data at that time
{...} <-// delete the image from storage with getReference link (link is string doc.data().urlProfile);
//Delete from firestore
doc.ref.
delete()
.then(response=>{
console.log('Document deleted successful', response);
})
.catch(error=>{
console.log('Error ocurred while data delete', error);
});
});
})
.catch(error =>{
console.log('Error while get the documents', error);
});
});
我正在使用打字稿来编写云函数
答案 0 :(得分:1)
Cloud Storage Server SDK不提供等效的getReferenceFromUrl。那只是一个客户端SDK操作。
您可能应该做的是将文件的完整路径与URL一起存储在存储桶中,并使用该路径删除对象,而不是由客户端SDK生成的URL。因此,对于Android客户端,您将存储StorageReference.getPath()的值,然后使用Bucket.file()将其提供给存储SDK,以建立对delete the object的另一个引用。