ios - 如何从 firebase 存储中删除文件夹?

标签 ios swift firebase firebase-storage

我尝试使用以下函数来完成此任务。但问题是,由于某种原因,这样做会导致 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.

最佳答案

我能够使用这个后端函数来完成这项工作:

    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`);
      }
    });
}

关于ios - 如何从 firebase 存储中删除文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56828892/

相关文章:

ios - 如何将 UIImageView 放在 tableView 的中心

ios - 用户注销后 Firebase Facebook 登录按钮不会更改

android - android : what is different between notification and data payload 的通知

ios - 具有强引用的 View Controller 导致内存泄漏

ios - UIView 未以编程方式显示在 Swift 中的 UIViewcollection 中

firebase - 使用 Firebase 模拟删除

ios - 使用 tabBar 从应用程序中的 navBar 推送 View

ios - 同时解析 UITapGestureRecognizer 和 UILongPressGestureRecognizer 并在手指按下时触发 UIGestureRecognizer.State.began

objective-c - RestKit iOS : Simple request error

ios - 如何使用代码在模拟器上模拟不良的互联网连接