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 = "http://oss-cn-hangzhou.aliyuncs.com";
// 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 https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";

// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(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>");
System.out.println(found);

// Close your OSSClient.
ossClient.shutdown();