Create Bucket
Updated at:2025-11-03
Basic workflow
- Instantiate the BOSClient class.
- To execute the BOSClient.createBucket() method, you need to specify the bucket name.
- From the returned object, use getLocation()/getName() to obtain the bucket's region and name.
Example code
Java
1BosClient client = new BosClient(config); //Create a BOSClient instance
2 client.createBucket(<BucketName>); //Create a bucket and specify the bucket name
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;
9import com.baidubce.services.bos.model.CreateBucketResponse;
10public class ExampleActivity extends Activity {
11 private static String bucketName = <BucketName>;
12 @Override
13 protected void onCreate(Bundle savedInstanceState) {
14 super.onCreate(savedInstanceState);
15 setContentView(R.layout.activity_main);
16 new Thread(new Runnable() {
17 @Override-9
18 public void run() {
19 try {
20 BosClientConfiguration config = new BosClientConfiguration();
21 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); //Your original AK/SK
22 config.setEndpoint(<EndPoint>); //the region where the bucket is located
23 BosClient client = new BosClient(config);
24 CreateBucketResponse response = client.createBucket(<BucketName>); //Create a bucket and specify the bucket name
25 System.out.println(response.getLocation());
26 System.out.println(response.getName());
27 } catch (BceServiceException e) {
28 System.out.println("Error ErrorCode: " + e.getErrorCode());
29 System.out.println("Error RequestId: " + e.getRequestId());
30 System.out.println("Error StatusCode: " + e.getStatusCode());
31 System.out.println("Error Message: " + e.getMessage());
32 System.out.println("Error ErrorType: " + e.getErrorType());
33 } catch (BceClientException e) {
34 System.out.println("Error Message: " + e.getMessage());
35 }
36 }
37 }).start();
38 }
39 }
Note: Since bucket names are unique across all regions, you must ensure that the BucketName does not conflict with BucketNames in any other region.
