javaamazon-s3sdkminio

How to check if object exist in MinIO bucket using MinIO Java SDK?


I currently working on a Spring web app with MinIO object storage. And i need to implement a function that check if upload file is successfully upload or not or check if the file exist in the bucket.

And from my research every information I found are outdated and cannot be use with latest MinIO Java SDK. That why I decide to ask this question here.


Solution

  • After digging through MinIO Java SDK for a while. I found that if you call statObject function in the minioClient, it will only be successful when object exist in the bucket. And throw an ErrorResponseException when object doesn't exist.

    So I came up with this function.

    public boolean isObjectExist(String name) {
            try {
                minioClient.statObject(StatObjectArgs.builder()
                        .bucket(defaultBucketName)
                        .object(name).build());
                return true;
            } catch (ErrorResponseException e) {
                e.printStackTrace();
                return false;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        }