Bucket management
Updated at:2025-11-03
Create Bucket
-
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
Java1BosClient client = new BosClient(config); //Create a BOSClient instance 2 client.createBucket(<BucketName>); //Create a bucket and specify the bucket name -
Complete example
Java1import 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 }Plain Text1> **Note:** Since bucket names are unique across all regions, you must ensure that the BucketName does not conflict with BucketNames in any other region.
View bucket list
-
Basic workflow
- Instantiate the BOSClient class.
- Calling the BOSClient.listBuckets() method will return an instance of the ListBucketsResponse class.
- For an instance of the ListBucketsResponse class, you can perform actions like getBuckets(), getOwner(), and getMetadata().
-
Example code
The following code can list all buckets of a user:
Java1List<BucketSummary> buckets = client.listBuckets().getBuckets(); 2 // Traverse bucket 3for (BucketSummary bucket : buckets) { 4 System.out.println(bucket.getName()); 5 }The following code can list the bucket's owner:
Java1User usr = client.listBuckets().getOwner(); 2System.out.println(usr.getDisplayName()); 3System.out.println(usr.getId());The following code can list the mucket's metadata:
Java1BosResponseMetadata metaData = client.listBuckets().getMetadata(); 2System.out.println(metaData.getBceRequestId()); 3System.out.println(metaData.getBceContentSha256()); 4System.out.println(metaData.getContentLength()); 5System.out.println(metaData.getEtag()); 6System.out.println(metaData.getExpires()); 7System.out.println(metaData.getContentMd5()); 8System.out.println(metaData.getContentDisposition()); 9System.out.println(metaData.getLastModified());Plain Text1> **Note**: If these parameters are not set for the bucket, the returned value may be null. -
Complete example
Java1import java.util.List; 2import android.app.Activity; 3import android.os.Bundle; 4import com.baidubce.BceClientException; 5import com.baidubce.BceServiceException; 6import com.baidubce.auth.DefaultBceCredentials; 7import com.baidubce.development.R; 8import com.baidubce.model.User; 9import com.baidubce.services.bos.BosClient; 10import com.baidubce.services.bos.BosClientConfiguration; 11import com.baidubce.services.bos.model.BosResponseMetadata; 12import com.baidubce.services.bos.model.BucketSummary; 13import com.baidubce.services.bos.model.CreateBucketResponse; 14import com.baidubce.services.bos.model.ListBucketsResponse; 15 16public class ExampleActivity extends Activity { 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 new Thread(new Runnable() { 22 @Override 23 public void run() { 24 try { 25 BosClientConfiguration config = new BosClientConfiguration(); 26 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 27 config.setEndpoint(<EndPoint>); 28 BosClient client = new BosClient(config); 29 ListBucketsResponse bucketsResponse = client.listBuckets() ; 30 // Retrieve the bucket list 31 List<BucketSummary> buckets = bucketsResponse.getBuckets(); 32 for (BucketSummary bucket : buckets) { 33 System.out.println(bucket.getName()); 34 } 35 // Retrieve the bucket's owner 36 User user = bucketsResponse.getOwner(); 37 System.out.println(user.getDisplayName()); 38 System.out.println(user.getId()); 39 } catch (BceServiceException e) { 40 System.out.println("Error ErrorCode: " + e.getErrorCode()); 41 System.out.println("Error RequestId: " + e.getRequestId()); 42 System.out.println("Error StatusCode: " + e.getStatusCode()); 43 System.out.println("Error Message: " + e.getMessage()); 44 System.out.println("Error ErrorType: " + e.getErrorType()); 45 } catch (BceClientException e) { 46 System.out.println("Error Message: " + e.getMessage()); 47 } 48 } 49 }).start(); 50} 51}
Check if bucket exists
-
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
Java1// 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
Java1import 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}
Delete bucket
-
Basic workflow
- Instantiate the BOSClient class.
- Call the BOSClient.deleteBucket() method.
- The deleteBucket() method does not return any value. If the deletion fails, an exception will be thrown.
-
Example code
Java1client.deleteBucket(<BucketName>); //Specify bucket namePlain Text1> **Note:** A bucket cannot be deleted if it is not empty (i.e., it contains objects or unfinished multipart uploads). The bucket must be emptied before successful deletion. -
Complete example
Java1import 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>; 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 18 public void run() { 19 try { 20 BosClientConfiguration config = new BosClientConfiguration(); 21 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 22 config.setEndpoint(<EndPoint>); 23 BosClient client = new BosClient(config); 24 // Delete bucket 25 client.deleteBucket(<BucketName>); //Specify bucket name 26 } catch (BceServiceException e) { 27 System.out.println("Error ErrorCode: " + e.getErrorCode()); 28 System.out.println("Error RequestId: " + e.getRequestId()); 29 System.out.println("Error StatusCode: " + e.getStatusCode()); 30 System.out.println("Error Message: " + e.getMessage()); 31 System.out.println("Error ErrorType: " + e.getErrorType()); 32 } catch (BceClientException e) { 33 System.out.println("Error Message: " + e.getMessage()); 34 } 35 } 36 }).start(); 37} 38}
Bucket permission control
Set bucket access permission
-
Basic workflow
- Instantiate the BOSClient class.
- Run the BOSClient.setBucketAcl() method.
- The setBucketAcl() method does not return any value. If the setting fails, an exception will be thrown.
-
Example code
Java1// Set bucket access permission 2client.setBucketAcl(<BucketName>, CannedAccessControlList.Private);Plain Text1> **Note: **CannedAccessControlList an enumerated type and contains three values: `Private`, `PublicRead` and `PublicReadWrite`, which correspond to relevant permissions respectively. For details, refer to BOS API Documentation - [Permission Control Using CannedAcl](BOS/API Reference/Access control.md#Permission control by uploading ACL files). -
Complete example
Java1import org.json.JSONException; 2import android.app.Activity; 3import android.os.Bundle; 4import com.baidubce.BceClientException; 5import com.baidubce.BceServiceException; 6import com.baidubce.auth.DefaultBceCredentials; 7import com.baidubce.development.R; 8import com.baidubce.services.bos.BosClient; 9import com.baidubce.services.bos.BosClientConfiguration; 10import com.baidubce.services.bos.model.CannedAccessControlList; 11 12public class ExampleActivity extends Activity { 13 private String bucketName = <BucketName>; 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 new Thread(new Runnable() { 19 @Override 20 public void run() { 21 try { 22 BosClientConfiguration config = new BosClientConfiguration(); 23 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 24 config.setEndpoint(<EndPoint>); 25 BosClient client = new BosClient(config); 26 // Set bucket access permission 27 client.setBucketAcl(<BucketName>, CannedAccessControlList.Private); //Specify the bucket name and set the bucket permission to private 28 } catch (BceServiceException e) { 29 System.out.println("Error ErrorCode: " + e.getErrorCode()); 30 System.out.println("Error RequestId: " + e.getRequestId()); 31 System.out.println("Error StatusCode: " + e.getStatusCode()); 32 System.out.println("Error Message: " + e.getMessage()); 33 System.out.println("Error ErrorType: " + e.getErrorType()); 34 } catch (BceClientException e) { 35 System.out.println("Error Message: " + e.getMessage()); 36 } catch (JSONException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 } 41 }).start(); 42} 43}
Set access permissions for a specific user on the bucket
-
Basic workflow
- Instantiate the BOSClient class.
- To run the BOSClient.setBucketAcl() method, you should create an instance of SetBucketAclRequest to provide the required information.
- The setBucketAcl() method does not return any value. If the setting fails, an exception will be thrown.
-
Example code
Java1List<Grant> grants = new ArrayList<Grant>(); 2List<Grantee> grantee = new ArrayList<Grantee>(); 3List<Permission> permission = new ArrayList<Permission>(); 4 //Grant permission to user 5grantee.add(new Grantee(<UserID>); 6 //Set permissions 7permission.add(Permission.READ); 8grants.add(new Grant().withGrantee(grantee).withPermission(permission)); 9 //Encapsulate into request 10SetBucketAclRequest request = new SetBucketAclRequest(<BucketName>, grants); 11 //Set bucket access permission 12client.setBucketAcl(request);Plain Text1> **Note**: The permission settings in Permission include three values: `READ`, `WRITE` and `FULL_CONTROL`, which correspond to relevant permissions respectively. For details, refer to BOS API Documentation - [Permission Control via Uploading ACL Files](BOS/API Reference/Access control.md#Permission control by uploading ACL files). -
Complete example
Java1import java.util.ArrayList; 2import java.util.List; 3import org.json.JSONException; 4import android.app.Activity; 5import android.os.Bundle; 6import com.baidubce.BceClientException; 7import com.baidubce.BceServiceException; 8import com.baidubce.auth.DefaultBceCredentials; 9import com.baidubce.development.R; 10import com.baidubce.services.bos.BosClient; 11import com.baidubce.services.bos.BosClientConfiguration; 12import com.baidubce.services.bos.model.Grant; 13import com.baidubce.services.bos.model.Grantee; 14import com.baidubce.services.bos.model.Permission; 15import com.baidubce.services.bos.model.SetBucketAclRequest; 16 17public class ExampleActivity extends Activity { 18 private String bucketName = <BucektName>; 19 List<Grant> grants = new ArrayList<Grant>(); 20 List<Grantee> grantee = new ArrayList<Grantee>(); 21 List<Permission> permission = new ArrayList<Permission>(); 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 new Thread(new Runnable() { 27 @Override 28 public void run() { 29 try { 30 BosClientConfiguration config = new BosClientConfiguration(); 31 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 32 config.setEndpoint(<EndPoint>); 33 BosClient client = new BosClient(config); 34 //Grant permission to specific user 35 grantee.add(new Grantee("UserId_1")); 36 grantee.add(new Grantee("UserId_2")); 37 //Grant permission to Everyone 38 grantee.add(new Grantee("*")); 39 //Set permissions 40 permission.add(Permission.READ); 41 permission.add(Permission.WRITE); 42 grants.add(new Grant().withGrantee(grantee).withPermission(permission)); 43 //Encapsulate request 44 SetBucketAclRequest request = new SetBucketAclRequest(<BucketName>, grants); 45 //Set bucket access permission 46 client.setBucketAcl(request); 47 } catch (BceServiceException e) { 48 System.out.println("Error ErrorCode: " + e.getErrorCode()); 49 System.out.println("Error RequestId: " + e.getRequestId()); 50 System.out.println("Error StatusCode: " + e.getStatusCode()); 51 System.out.println("Error Message: " + e.getMessage()); 52 System.out.println("Error ErrorType: " + e.getErrorType()); 53 } catch (BceClientException e) { 54 System.out.println("Error Message: " + e.getMessage()); 55 } catch (JSONException e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } 59 } 60 }).start(); 61} 62}
