All Products
Search
Document Center

Object Storage Service:Determine whether an object exists (Android SDK)

Last Updated:Mar 20, 2026

The OSS SDK for Android provides a synchronous API to check whether a specific object exists in a bucket.

Prerequisites

Before you begin, make sure that you have:

Check whether an object exists

Method signature

public boolean doesObjectExist(String bucketName, String key) throws ClientException, ServiceException;

Parameters

ParameterTypeDescription
bucketNameStringThe name of the bucket.
keyStringThe full path of the object, excluding the bucket name. Example: exampledir/exampleobject.txt.

Sample code

try {
    // Specify the bucket name and the full path of the object.
    // In this example, the bucket is examplebucket and the object path is exampledir/exampleobject.txt.
    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-side exceptions, such as network exceptions.
    e.printStackTrace();
} catch (ServiceException e) {
    // Handle server-side exceptions.
    Log.e("ErrorCode", e.getErrorCode());
    Log.e("RequestId", e.getRequestId());
    Log.e("HostId", e.getHostId());
    Log.e("RawMessage", e.getRawMessage());
}

Return value

OutcomeResult
Object existsReturns true
Object does not existReturns false
Request failsThrows ClientException (client-side errors, such as network exceptions) or ServiceException (server-side errors)

What's next