编程语言
首页 > 编程语言> > java-使用Google Cloud Storage从Firebase存储中删除文件夹

java-使用Google Cloud Storage从Firebase存储中删除文件夹

作者:互联网

我想删除文件夹“ test”及其中的所有内容.

我可以使用以下代码通过终端成功删除FirebaseStorage中的文件夹及其所有内容/子文件夹:

gsutil rm -r gs://bucketname.appspot.com/test/**

enter image description here

但是,当我尝试在Java中执行此操作时,它不起作用.

    Storage storage = StorageOptions.getDefaultInstance().getService();
    String bucketName = "bucketname.appspot.com/test";
    Bucket bucket = storage.get(bucketName);
    bucket.delete(Bucket.BucketSourceOption.metagenerationMatch());

它引发以下异常:

Exception in thread "FirebaseDatabaseEventTarget" com.google.cloud.storage.StorageException: Invalid bucket name: 'bucketname.appspot.com/test'
    at com.google.cloud.storage.spi.DefaultStorageRpc.translate(DefaultStorageRpc.java:202)
    at com.google.cloud.storage.spi.DefaultStorageRpc.get(DefaultStorageRpc.java:322)
    at com.google.cloud.storage.StorageImpl$4.call(StorageImpl.java:164)
    at com.google.cloud.storage.StorageImpl$4.call(StorageImpl.java:161)
    at com.google.cloud.RetryHelper.doRetry(RetryHelper.java:179)
    at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:244)
    at com.google.cloud.storage.StorageImpl.get(StorageImpl.java:160)
    at xxx.backend.server_request.GroupRequestManager.deleteGroupStorage(GroupRequestManager.java:119)
    at xxx.backend.server_request.GroupRequestManager.deleteGroup(GroupRequestManager.java:26)
    at xxx.backend.server_request.ServerRequestListener.onChildAdded(ServerRequestListener.java:27)
    at com.google.firebase.database.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:65)
    at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:49)
    at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:41)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Invalid bucket name: 'bucketname.appspot.com/test'",
    "reason" : "invalid"
  } ],
  "message" : "Invalid bucket name: 'bucketname.appspot.com/test'"
}

那么,它不存在吗?因为当我不使用/ test运行此代码时:

    Storage storage = StorageOptions.getDefaultInstance().getService();
    String bucketName = "bucketname.appspot.com";
    Bucket bucket = storage.get(bucketName);
    bucket.exists(Bucket.BucketSourceOption.metagenerationMatch());

然后exist将返回true,没有异常,并且我能够列出所有blob..但是我想删除“ / test”中的所有内容.

编辑:好的,我确实使它像这样工作,但是我需要使用迭代器.有更好的解决方案吗?通配符之类的?

    Storage storage = StorageOptions.getDefaultInstance().getService();
    String bucketName = "bucketname.appspot.com";
    Page<Blob> blobPage = storage.list(bucketName, Storage.BlobListOption.prefix("test/"));
    List<BlobId> blobIdList = new LinkedList<>();
    for (Blob blob : blobPage.iterateAll()) {
        blobIdList.add(blob.getBlobId());
    }
    storage.delete(blobIdList);

解决方法:

桶是保存数据的基本容器.您有一个名称为“ bucketname.appspot.com”的存储桶. “ bucketname.appspot.com/test”是您的存储桶名称加上一个文件夹,因此它不是您的存储桶的有效名称.通过调用bucket.delete(…),您只能删除整个存储桶,但不能删除存储桶中的文件夹.使用GcsService删除文件或文件夹.

String bucketName = "bucketname.appspot.com";
GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
gcsService.delete(new GcsFilename(bucketName, "test"));

标签:firebase-admin,firebase,google-cloud-storage,firebase-storage,java
来源: https://codeday.me/bug/20191111/2019799.html