This topic describes how to determine whether a specified object exists

Run the following code to determine whether a specified object exists:

// This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";

// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Determine whether the specified object exists. The doesObjectExist includes a parameter: isOnlyInOSS. If the value of this parameter is true, 302 Redirect or OSS mirroring is ignored. Otherwise, 302 Redirect or OSS mirroring is considered.

boolean found = ossClient.doesObjectExist("<yourBucketName>", "<yourObjectName>");

// Close your OSSClient.