FirebaseStorage:如何删除目录

FirebaseStorage: How to Delete Directory

当我尝试删除目录时,FirebaseStorage 总是 returns 错误 400,即类似下面的内容总是 returns 错误 400.

let storageRef = FIRStorage.storage().reference().child("path/to/directory")
storageRef.deleteWithCompletion { (error) in
    print("error: \(error)") // always prints error code 400
}

但是,删除文件效果很好,例如类似 return 的错误:

let storageRef = FIRStorage.storage().reference().child("path/to/file.jpg")
storageRef.deleteWithCompletion { (error) in
    print("error: \(error)") // works fine, error is nil
}

我在这里做错了什么?我认为 FirebaseStorage 不支持它,因为从一个目录中一个一个地删除文件会很蹩脚(特别是如果所述目录有 100 或 1000 个这样的文件)。

来自 google 组,无法删除目录。你必须在某个地方(在 Firebase 数据库中)维护一个文件列表,然后一个一个地删除它们。

https://groups.google.com/forum/#!topic/firebase-talk/aG7GSR7kVtw

我也提交了功能请求,但由于他们的错误跟踪器不是 public,所以没有 link 我可以分享。

In 26/5/2017 there is no way to delete directory But you can use my algorithm

使用此代码。

   this.sliders = this.db.list(`users/${this.USER_UID}/website/sliders`) as FirebaseListObservable<Slider[]>



  /**
   * Delete image from firebase storage is take a string path of the image
   * @param _image_path
   */
  deleteImage(_image_path: string) {

    // first delete the image
    const storageRef = firebase.storage().ref();
    const imageRef = storageRef.child(_image_path);
    imageRef.delete().then(function() {
      console.log('file deleted');
      // File deleted successfully
    }).catch(function(error) {
      // Uh-oh, an error occurred!
      console.log(error);
    });

  }



  /**
   * Deletes multiple Sliders, it takes an array of ids
   * @param ids
   */
  deleteMutipleSliders(ids: any) {

    ids.forEach(id => {

      this.getSliderDetails(id).subscribe(slider => {

        let id = slider.$key; // i think this is not nesesery
        const imgPath = slider.path;

        this.deleteImage(imgPath);
      });

      return this.sliders.remove(id);

    });


  }

如上所述,删除目录无效。我正在分享一个查询 Firebase 数据库中的文件列表并一个一个删除它们的示例。这是我的询问电话。

    let messagePhotoQuery = messagesRef.child(group.key).child("messages").queryOrdered(byChild: "photoURL")
    deleteMessagePhotos(from: messagePhotoQuery)

这是我的函数,循环获取 URL,然后删除该存储引用中的文件。

    func deleteMessagePhotos(from photoQuery: FIRDatabaseQuery) {
    photoQuery.observeSingleEvent(of: .value, with: { (messagesSnapshot) in
        guard messagesSnapshot.exists() else { return }
        print(messagesSnapshot)
        for message in messagesSnapshot.children {
            let messageSnapshot = message as! FIRDataSnapshot
            let messageData = messageSnapshot.value as! [String: AnyObject]
            if let photoURL = messageData["photoURL"] as? String {
                let photoStorageRef = FIRStorage.storage().reference(forURL: photoURL)
                photoStorageRef.delete(completion: { (error) in
                    if let error = error {
                        print(error)
                    } else {
                        // success
                        print("deleted \(photoURL)")
                    }
                })
            }
        }
    })
}

从安全 google 云功能的上下文中 - 您可以使用 Google Cloud Storage npm 包(又名 Google Cloud Storage API 删除整个目录) 像这样:

const gcs = require('@google-cloud/storage')();
const functions = require('firebase-functions');
...
  const bucket = gcs.bucket(functions.config().firebase.storageBucket);

  return bucket.deleteFiles({
    prefix: `users/${userId}/`
  }, function(err) {
    if (err) {
      console.log(err);
    } else {
      console.log(`All the Firebase Storage files in users/${userId}/ have been deleted`);
    }
  });

更多文档可用on GCS API docs

这是使用 Firebase 函数删除 Firebase 存储文件夹中文件的一种解决方案。

假设您的模型存储在 Firebase 数据库中的 /MyStorageFilesInDatabaseTrackedHere/path1/path2 下。

这些模型将有一个名为 "filename" 的字段,该字段将具有 Firebase 存储中文件的名称。

工作流程是:

  1. 删除 Firebase 数据库中包含模型列表的文件夹
  2. 通过 Firebase 函数监听该文件夹的删除
  3. 此函数将遍历文件夹的子文件夹,获取文件名并将其删除到存储中。

(免责声明:存储中的文件夹在此函数结束时仍然存在,因此需要再次调用以将其删除。)

// 1. Define your Firebase Function to listen for deletions on your path
exports.myFilesDeleted = functions.database
    .ref('/MyStorageFilesInDatabaseTrackedHere/{dbpath1}/{dbpath2}')
    .onDelete((change, context) => {

// 2. Create an empty array that you will populate with promises later
var allPromises = [];

// 3. Define the root path to the folder containing files
// You will append the file name later
var photoPathInStorageRoot = '/MyStorageTopLevelFolder/' + context.params.dbpath1 + "/" + context.params.dbpath2;

// 4. Get a reference to your Firebase Storage bucket
var fbBucket = admin.storage().bucket();

// 5. "change" is the snapshot containing all the changed data from your
// Firebase Database folder containing your models. Each child has a model
// containing your file filename
if (change.hasChildren()) {
    change.forEach(snapshot => {

        // 6. Get the filename from the model and
        // form the fully qualified path to your file in Storage
        var filenameInStorage = photoPathInStorageRoot + "/" + snapshot.val().filename;

        // 7. Create reference to that file in the bucket
        var fbBucketPath = fbBucket.file(filenameInStorage);

        // 8. Create a promise to delete the file and add it to the array
        allPromises.push(fbBucketPath.delete());
    });
}

// 9. Resolve all the promises (i.e. delete all the files in Storage)
return Promise.all(allPromises);
});

看来删除目录的唯一方法就是循环遍历所有文件,一个一个删除:

async function DeleteFirebaseStorageFolder(directoryName: string) {
    const serviceAccount = require('../secret/adminSdkPrivateKey.json');

    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        storageBucket: 'gs://yourprojectname.appspot.com'
    });

    const bucket = admin.storage().bucket();
    const files = (await bucket.getFiles({ directory: folderName }))[0];

    const Deletions = [];

    files.forEach(file => {
        Deletions.push(bucket.file(file.name).delete());
    })

    await Promise.all(Deletions);

    bucket.getFiles({ directory: folderName }).then((files) => {
        console.log('Remaining number of files: ' + (files[0].length).toString());
    })
}
DeleteFirebaseStorageFolder('myDirectoryName');

您可以递归列出文件夹并删除其内容:

deleteFolder(path) {
  const ref = firebase.storage().ref(path);
  ref.listAll()
    .then(dir => {
      dir.items.forEach(fileRef => this.deleteFile(ref.fullPath, fileRef.name));
      dir.prefixes.forEach(folderRef => this.deleteFolder(folderRef.fullPath))
    })
    .catch(error => console.log(error));
}

deleteFile(pathToFile, fileName) {
  const ref = firebase.storage().ref(pathToFile);
  const childRef = ref.child(fileName);
  childRef.delete()
}

执行此操作的方法如下(对于最后一个文件,删除目录):

let ref = firebase.storage().ref(path);
ref.listAll().then(dir => {
  dir.items.forEach(fileRef => {
    var dirRef = firebase.storage().ref(fileRef.fullPath);
    dirRef.getDownloadURL().then(function(url) {
      var imgRef = firebase.storage().refFromURL(url);
      imgRef.delete().then(function() {
        // File deleted successfully 
      }).catch(function(error) {
        // There has been an error      
      });
    });
  });
}).catch(error => {
  console.log(error);
});

'Miki' 答案的现代版本,使用 ES2018 Async/Await 以及 for await... of.. 删除文件计数器.

const storage = firebase.storage();

const deleteFile = async (filePath) => {
    const ref = storage.ref(filePath);
    return await ref.delete();
};
const deleteFolderRecursive = async (folderPath) => {
    const ref = storage.ref(folderPath);
    const list = await ref.listAll();
    let filesDeleted = 0;

    for await (const fileRef of list.items) {
        await deleteFile(fileRef.fullPath);
        filesDeleted++;
    }
    for await (const folderRef of list.prefixes) {
        filesDeleted += await deleteFolderRecursive(folderRef.fullPath);
    }
    return filesDeleted;
};

// Usage
async deleteFolder(x) {
    try {
         const filesDeleted = deleteFolderRecursive('path/to/storage/folder');
         console.log(`${filesDeleted} files has been deleted`);
         // you can now, for instance, unblock the UI at this point
    } catch(err){
       // probably denied permissions or 'path/to/storage/folder' is not a folder
       console.error(err)
    }

}

如前所述,文件夹引用不会被删除但至少它们不会像无法访问的文件那样从您的存储配额中拖走。

根据 TheFastCat 的回答和 Teodor Ciuraru 的评论,我创建了以下 Firebase 云函数,它删除了用户的文件夹当用户从 Firebase 身份验证中删除时。

const admin = require('firebase-admin')
const functions = require('firebase-functions')

const bucket = admin.storage().bucket()

exports.deleteUser = functions.auth.user().onDelete(async (user) => {
    const { uid } = user
    await bucket.deleteFiles({
        prefix: `${uid}/`
    })
})

2020 年,删除 Firebase 存储文件夹似乎奏效了。我刚刚在我的 Cloud Functions (Node.js Admin) 中执行了以下操作,它删除了其中的文件夹、子文件夹和文件。我确实使用 npm 下载了 Google Cloud Storage,但我并没有在物理上以任何方式导入该库,而且 Firebase Storage 现在似乎支持此功能,这与上面大家所说的不同。也许他们已经更新了它。我测试了它是有效的。

admin.initializeApp({
    storageBucket: "bucket_name.appspot.com",
})

const bucket = admin.storage().bucket();
await bucket.deleteFiles({
   prefix: `${folderName}/`
});

现在您知道我们无法删除 firebase 存储中的文件夹。但是您可以列出该文件夹中的文件并删除每个文件。

这里有一个例子..

        // Create a root reference
        var storageRef = firebase.storage().ref();
        // Create a reference 
        var mountainsRef = storageRef.child("your root path");

        // Now we get the references of these files
        mountainsRef.listAll().then(function (result) {
            result.items.forEach(function (file) {
               file.delete();
            });
        }).catch(function (error) {
            // Handle any errors
        });

Flutter firebase storage listAll()方法目前在dev版本的包中可用,在5.0.0-dev.1版本中可以使用,如上所示

我注意到,当删除 firebase 存储文件夹中的所有文件时,一段时间后,该文件夹也会被删除。无需任何操作。

不要在家里这样做!

Firebase 9

如何使用客户端删除文件夹的示例:

  • 如果有数百万个文件,不推荐可能不起作用。
async function deleteFolder(path: string) {
    const folderRef = ref(storage, path)
    const fileList = await listAll(folderRef)
    const promises = []
    for(let item of fileList.items) {
        promises.push(deleteObject(item))
    }
    const result = await Promise.all(promises)
    return result
}

空文件夹会自行删除。所以从文件夹中删除所有文件也会删除文件夹本身。