Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if Bucket.exists()? #1133

Closed
raintears opened this issue Jul 29, 2016 · 3 comments
Closed

Check if Bucket.exists()? #1133

raintears opened this issue Jul 29, 2016 · 3 comments
Assignees

Comments

@raintears
Copy link

Hi,

I still can't understand how can I use bucket.exists from the documentation. I don't even know how to instantiate a new bucket. This is what I did.

Bucket bucket = new Bucket("bucketname");
return bucket.exists();

Apparently I can't instantiate a new bucket like this, so how then can I use bucket.exists()?

@mziccard
Copy link
Contributor

mziccard commented Aug 8, 2016

Hi @raintears,

Bucket is a class that contains all information of a bucket, in contrast with BucketInfo, the Bucket class also exposes methods to update and delete the bucket. Bucket objects cannot be created directly but are returned by Storage methods like create and get.

To check for the existence of a bucket if you do not have an instance of Bucket you can do the following:

return storage.get("bucketname", Storage.BucketGetOption.fields()) != null;

storage.get will return a Bucket object or null if not found. Storage.BucketGetOption.fields() tells the service to return only the bucket name (we don't need to transfer all bucket's fields to check for existence).

@raintears
Copy link
Author

Thanks @mziccard

This explanation is more straightforward!

@kaus22
Copy link

kaus22 commented Jul 24, 2018

@mziccard which package did you get your Storage class from?
I'm using com.google.api.services.storage.Storage and BucketGetOptions.fields() is not getting resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants