Soumik Das
Soumik Das

Reputation: 167

Java - How to check if a GCS storage object exist in a bucket?

I have a bucket named <Bucket_Name>. I want to check if "User-Info.txt" file exist in <Bucket_Name> or not.

How do I achieve it using Java.

Upvotes: 2

Views: 2800

Answers (1)

Soumik Das
Soumik Das

Reputation: 167

I sort out the requirement by following the below logic:

    public Boolean isObjectExist(String sourceBucketName, String projectId){
            String objectName = "User-Info.txt";
            Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
            Bucket bucket = storage.get(sourceBucketName);
                Blob blob = storage.get(sourceBucketName,objectName);
            if (blob != null && blob.exists()){
            return true;
            } else
            return false;
            }

Upvotes: 1

Related Questions