0

Is there any way i can get the size of a bucket from the google cloud in java.

I know you can get it like this:

gsutil du -s gs://[BUCKET_NAME]/

Is it possible to somehow obtain the total size of a bucket thorugh the java API?

Felpower
  • 87
  • 1
  • 12

1 Answers1

1

There is no function to get the size of a bucket with java API google-cloud-java.

You can iterate over blobs in the bucket and calculate the total size using blob.getSize() but it would be very slow.

According to the official documentation, gsutil du can take a long time for large buckets Getting bucket information:

Caution: The gsutil du command calculates space usage by making object listing requests, which can take a long time for large buckets. If the number of objects in your bucket is hundreds of thousands or more, use Stackdriver instead

An alternative solution would be to enable the bucket logging and check the size of the bucket from the logs:

Setting up log delivery

Carefully calculating Google Cloud Storage Buckets size with Cloud Functions & Pub/Sub

Here you can find a related question link.

marian.vladoi
  • 7,663
  • 1
  • 15
  • 29
  • Thanks for the information. I actually need to delete buckets via a job which i implemented in java spring. But the api can not delete buckets that are not empty. – Felpower Dec 16 '19 at 07:48