All Products
Search
Document Center

Object Storage Service:Determine whether an object exists by using OSS SDK for Android

Last Updated:Feb 20, 2024

Object Storage Service (OSS) SDK for Android provides a convenient synchronous API to check whether a specific object exists in a 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.

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.

Examples

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

try {
     // Specify the name of the bucket and the full path of the object. In this example, the bucket name is examplebucket and the full path of the object is exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
    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. 
    e.printStackTrace();
} 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());
}

References