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

Any way to confirm object exist or not? #639

Closed
laudukang opened this issue Nov 1, 2017 · 10 comments
Closed

Any way to confirm object exist or not? #639

laudukang opened this issue Nov 1, 2017 · 10 comments
Assignees

Comments

@laudukang
Copy link

laudukang commented Nov 1, 2017

I have a use case to comfirm a object exist in bucket whether or not, and I can finish the case through the following ways:

  1. MinioClient#statObject, method will throw exception if object not exist, so I can know the object is not exist in minio server

  2. MinioClient#getObject, method will throw exception if object not exist

Is there a better way that I can ensure a object exist in minio server whether or not?

@harshavardhana
Copy link
Member

What you explained is the most idiomatic way. What exactly are you looking for ?

@laudukang
Copy link
Author

@harshavardhana Sorry, I have updated the issue.

@balamurugana
Copy link
Member

balamurugana commented Nov 1, 2017

@laudukang You would need to do like below

boolean found = false;
try {
  minioClient.statObject("my-bucketname", "my-objectname");
  found = true;
} catch (ErrorResponseException e) {
  if (e.errorResponse().errorCode() != ErrorCode.NO_SUCH_OBJECT)) {
    throw e;
  }
}

if (found) {
  // Do things when object is found
} else {
  // Do things when object is not found
}

@kannappanr kannappanr self-assigned this Nov 1, 2017
@kannappanr
Copy link
Contributor

@laudukang Please close the issue, if the above reply is what you are looking for.

@balamurugana balamurugana changed the title Any way to confirm object exit or not? Any way to confirm object exist or not? Nov 1, 2017
@vmyrsky
Copy link

vmyrsky commented Apr 30, 2019

This works a bit better.

catch (ErrorResponseException e) {
  ErrorCode code = e.errorResponse().errorCode();
  if (code == ErrorCode.NO_SUCH_KEY || code == ErrorCode.NO_SUCH_OBJECT) {
    // Not found
  } else {
    throw e;
  }
}

@negarnegma
Copy link

what about a folder? it throws a NO SUCH KEY exception too!

@vishrantgupta
Copy link

vishrantgupta commented Nov 28, 2021

@balamurugana I guess there is a delete marker (at least in Go minio client, have not checked in Java), and instead of just returning true, we should check for a delete marker.

objectStats.IsDeleteMarker == false

@tabiStein
Copy link

Hello MinIO devs! After searching for the correct way to check if a MinIO object exists, I stumbled across this thread. @balamurugana suggested the following:

try {
  minioClient.statObject("my-bucketname", "my-objectname");
  found = true;
} catch (ErrorResponseException e) {
  if (e.errorResponse().errorCode() != ErrorCode.NO_SUCH_OBJECT)) {
    throw e;
  }
}

However, in the latest version of MinIO (or more specifically, since release 8.0.0) the ErrorCode enum has been deleted. (See PR #1028 ).
Is there a convenient and clean way to check the error codes without hardcoding them in my application?

Thanks!! - Tabi

@balamurugana
Copy link
Member

It is same logic like below

try {
  StatObjectResponse stat = minioClient.statObject(StatObjectArgs.builder().bucket("my-bucketname").object("my-objectname").build());
  found = true;
} catch (ErrorResponseException e) {
  if (e.response().code() != "NoSuchKey")) {
    throw e;
  }
}

Refer https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html for error codes. If you want enum style error codes, define your own enums and use it here.

@tabiStein
Copy link

Thanks for the quick reply and the link to the error codes @balamurugana !

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

8 participants