我试图通过波纹管功能来实现这一点。但是问题在于,由于某种原因,这样做会导致thumbRef.delete {
无法运行。相反,它只会永远循环。
我该如何进行这项工作?
注意:我要删除文件夹中的所有内容。如果我可以删除整个文件夹,则可以。
private var done = false
private func deletePostFromStorage(selectedPost: Post) {
let postID = selectedPost.media.postID
let uid = selectedPost.user.userID
print(postID, " tytytytyty ", uid)
// Create a reference to the file to delete
let storageRef = Storage.storage().reference().child(uid!).child(postID!)
var i = 1
while !done {
let thumbRef = storageRef.child("media\(i).jpg")
let vidRef2 = storageRef.child("media\(i).mov")
let imageRef3 = storageRef.child("media\(i)")
print(done, " INSIDE THE WHILE LOOP ", i)
// Delete the file
thumbRef.delete { error in
print("fhdjskfhdsakjfhkadsl")
if let error = error {
// Uh-oh, an error occurred!
print("thumb error in delete")
imageRef3.delete(completion: { (error) in
if let error = error {
//error occured no more medias (no vid no image and no thumbnail with this media)
self.done = true
print("image error in delete")
} else {
//successfull deletion
print("image deleted")
}
})
} else {
print("thumb delete succes")
// File deleted successfully
vidRef2.delete(completion: { (error) in
if let error = error {
//error occured
print("video error in delete")
} else {
//successfull deletion
print("video deleted")
}
})
}
if i > 30 {
//this is only for testing make sure its not endless
self.done = true
}
}
i+=1
}
}
我还在后端中试用此功能:
function deletePostFromStorage(uid: string, postID: string) {
const gcs = require('@google-cloud/storage')();
const functions = require('firebase-functions');
//const bucket = gcs.bucket(functions.config().firebase.storageBucket);
const bucket = admin.storage().bucket();
return bucket.deleteFiles({
prefix: `${uid}/${postID}/`
}, function(err) {
if (err) {
console.log(err);
} else {
console.log(`All the Firebase Storage files in ${uid}/${postID}/ have been deleted`);
}
});
}
但是我收到类似这样的错误:
ERROR: /Users/=/Desktop/FinalAppPrjv2/firefunctions/functions/src/index.ts:205:23 - Module '@google-cloud/storage' is not listed as dependency in package.json
ERROR: /Users/=/Desktop/FinalAppPrjv2/firefunctions/functions/src/index.ts:206:9 - Shadowed name: 'functions'
ERROR: /Users/=/Desktop/FinalAppPrjv2/firefunctions/functions/src/index.ts:211:12 - Expression has type `void`. Put it on its own line as a statement.
答案 0 :(得分:0)
我可以使用以下后端功能来完成这项工作:
function deletePostFromStorage(uid: string, postID: string) {
//const gcs = require('@google-cloud/storage')();
//const functions = require('firebase-functions');
//const bucket = gcs.bucket(functions.config().firebase.storageBucket);
const bucket = admin.storage().bucket();
bucket.deleteFiles({
prefix: `${uid}/${postID}/`
}, function(err) {
if (err) {
console.log(err);
} else {
console.log(`All the Firebase Storage files in ${uid}/${postID}/ have been deleted`);
}
});
}