Check if bucket exists
Updated at:2025-11-03
Basic workflow
- Instantiate the BOSClient class.
- Call the BOSClient.doesBucketExist() method.
- The doesBucketExist() method returns a boolean value to indicate whether the bucket exists.
Example code
Java
1// Retrieve bucket existence information
2 boolean exists = client.doesBucketExist(<BucketName>); //Specify the bucket name
3 // Output result
4if (exists) {
5 System.out.println("Bucket exists");
6} else {
7 System.out.println("Bucket not exists");
8}
Complete example
Java
1import android.app.Activity;
2import android.os.Bundle;
3import com.baidubce.BceClientException;
4import com.baidubce.BceServiceException;
5import com.baidubce.auth.DefaultBceCredentials;
6import com.baidubce.development.R;
7import com.baidubce.services.bos.BosClient;
8import com.baidubce.services.bos.BosClientConfiguration;
9
10public class ExampleActivity extends Activity {
11 private String bucketName = <BucketName>; //Your bucket name
12
13@Override
14protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.activity_main);
17 new Thread(new Runnable() {
18 @Override
19 public void run() {
20 try {
21 BosClientConfiguration config = new BosClientConfiguration();
22 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
23 config.setEndpoint(<EndPoint>);
24 BosClient client = new BosClient(config);
25 // Retrieve bucket existence information
26 boolean exists = client.doesBucketExist(<BucketName>); //Specify the bucket name
27 // Output result
28 if (exists) {
29 System.out.println("Bucket exists");
30 } else {
31 System.out.println("Bucket not exists");
32 }
33 } catch (BceServiceException e) {
34 System.out.println("Error ErrorCode: " + e.getErrorCode());
35 System.out.println("Error RequestId: " + e.getRequestId());
36 System.out.println("Error StatusCode: " + e.getStatusCode());
37 System.out.println("Error Message: " + e.getMessage());
38 System.out.println("Error ErrorType: " + e.getErrorType());
39 } catch (BceClientException e) {
40 System.out.println("Error Message: " + e.getMessage());
41 }
42 }
43 }).start();
44}
45}
