Deleting a folder from Firebase storage using google cloud storage
I want to delete the "test" folder and everything in it.
I can successfully delete a folder and all its contents / sub-folders in FirebaseStorage using this term:
gsutil rm -r gs://bucketname.appspot.com/test/**
However, when I tried to do it in java, it doesn't work.
Storage storage = StorageOptions.getDefaultInstance().getService();
String bucketName = "bucketname.appspot.com/test";
Bucket bucket = storage.get(bucketName);
bucket.delete(Bucket.BucketSourceOption.metagenerationMatch());
It throws this exception:
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'"
}
So it doesn't exist? because when i run this code without / test:
Storage storage = StorageOptions.getDefaultInstance().getService();
String bucketName = "bucketname.appspot.com";
Bucket bucket = storage.get(bucketName);
bucket.exists(Bucket.BucketSourceOption.metagenerationMatch());
then exists returns true, exception and I can list all blobs. But I want to remove everything inside "/ test".
Edit: Ok, I made it work, but I need to use an iterator. Is there a better solution? Wildcard or something else?
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);
source to share
Buckets are the main containers in which your data is stored. You have a bucket named "bucketname.appspot.com". "bucketname.appspot.com/test" is your bucket name plus folder, so this is not a valid bucket name. By calling bucket.delete(...)
, you can only delete the whole bucket, but you cannot delete the folder in the bucket. Use GcsService
to delete files or folders.
String bucketName = "bucketname.appspot.com";
GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
gcsService.delete(new GcsFilename(bucketName, "test"));
source to share