OSS SDK for Android provides a convenient synchronous API to check whether a specified object exists in the bucket.

Usage notes

To determine whether an object exists, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Sample code

The following code provides an example on how to check whether an object named exampleobject.txt exists in a bucket named examplebucket:

try {
     // Specify the bucket name such as examplebucket and the full path of the object such as exampledir/exampleobject.txt. The full path cannot contain the bucket name. 
    if (oss.doesObjectExist("examplebucket", "exampledir/exampleobject.txt")) {
        Log.d("doesObjectExist", "object exist.");
    } else {
        Log.d("doesObjectExist", "object does not exist.");
} catch (ClientException e) {
    // Handle client exceptions, such as network exceptions. 
} catch (ServiceException e) {
    // Handle service exceptions. 
    Log.e("ErrorCode", e.getErrorCode());
    Log.e("RequestId", e.getRequestId());
    Log.e("HostId", e.getHostId());
    Log.e("RawMessage", e.getRawMessage());