| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 54cd111 commit 6ca73d3
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,17 +16,20 @@ | |||
| 16 | 16 | ||
| 17 | 17 | package com.google.cloud.storage.testing; | |
| 18 | 18 | ||
| 19 | + import com.google.api.gax.paging.Page; | ||
| 20 | + import com.google.api.gax.retrying.RetrySettings; | ||
| 19 | 21 | import com.google.auth.oauth2.GoogleCredentials; | |
| 20 | 22 | import com.google.cloud.http.HttpTransportOptions; | |
| 21 | - import com.google.api.gax.retrying.RetrySettings; | ||
| 23 | + import com.google.cloud.storage.BlobId; | ||
| 22 | 24 | import com.google.cloud.storage.BlobInfo; | |
| 25 | + import com.google.cloud.storage.Bucket; | ||
| 23 | 26 | import com.google.cloud.storage.Storage; | |
| 24 | 27 | import com.google.cloud.storage.Storage.BlobListOption; | |
| 25 | 28 | import com.google.cloud.storage.StorageException; | |
| 26 | 29 | import com.google.cloud.storage.StorageOptions; | |
| 27 | - | ||
| 28 | 30 | import java.io.IOException; | |
| 29 | 31 | import java.io.InputStream; | |
| 32 | + import java.util.ArrayList; | ||
| 30 | 33 | import java.util.UUID; | |
| 31 | 34 | import java.util.concurrent.Callable; | |
| 32 | 35 | import java.util.concurrent.ExecutionException; | |
@@ -67,6 +70,33 @@ public StorageOptions getOptions() { | |||
| 67 | 70 | return options; | |
| 68 | 71 | } | |
| 69 | 72 | ||
| 73 | + public static void cleanBuckets(final Storage storage, final long olderThan, long timeoutMs) { | ||
| 74 | + Runnable task = | ||
| 75 | + new Runnable() { | ||
| 76 | + @Override | ||
| 77 | + public void run() { | ||
| 78 | + Page<Bucket> buckets = | ||
| 79 | + storage.list(Storage.BucketListOption.prefix(BUCKET_NAME_PREFIX)); | ||
| 80 | + for (Bucket bucket : buckets.iterateAll()) { | ||
| 81 | + if (bucket.getCreateTime() < olderThan) { | ||
| 82 | + try { | ||
| 83 | + forceDelete(storage, bucket.getName()); | ||
| 84 | + } catch (Exception e) { | ||
| 85 | + // Ignore the exception, maybe the bucket is being deleted by someone else. | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + }; | ||
| 91 | + Thread thread = new Thread(task); | ||
| 92 | + thread.start(); | ||
| 93 | + try { | ||
| 94 | + thread.join(timeoutMs); | ||
| 95 | + } catch (InterruptedException e) { | ||
| 96 | + log.info("cleanBuckets interrupted"); | ||
| 97 | + } | ||
| 98 | + } | ||
| 99 | + | ||
| 70 | 100 | /** | |
| 71 | 101 | * Deletes a bucket, even if non-empty. Objects in the bucket are listed and deleted until bucket | |
| 72 | 102 | * deletion succeeds or {@code timeout} expires. To allow for the timeout, this method uses a | |
@@ -184,8 +214,12 @@ public DeleteBucketTask(Storage storage, String bucket) { | |||
| 184 | 214 | @Override | |
| 185 | 215 | public Boolean call() { | |
| 186 | 216 | while (true) { | |
| 217 | + ArrayList<BlobId> ids = new ArrayList<>(); | ||
| 187 | 218 | for (BlobInfo info : storage.list(bucket, BlobListOption.versions(true)).getValues()) { | |
| 188 | - storage.delete(info.getBlobId()); | ||
| 219 | + ids.add(info.getBlobId()); | ||
| 220 | + } | ||
| 221 | + if (!ids.isEmpty()) { | ||
| 222 | + storage.delete(ids); | ||
| 189 | 223 | } | |
| 190 | 224 | try { | |
| 191 | 225 | storage.delete(bucket); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments