Bucket management
A bucket serves as both a namespace within BOS and a management entity for advanced features like billing, permission control, and log recording.
-
Bucket names are globally unique across all regions and cannot be changed after creation.
Notes Baidu AI Cloud currently supports multiple regions. Please refer to[Region Selection Guide](Reference/Region Selection Instructions/Region.md).
Currently, the supported regions include "North China-Beijing," "South China-Guangzhou" and "East China-Suzhou." Beijing region:
http://bj.bcebos.com, Guangzhou region:http://gz.bcebos.com, Suzhou region:http://su.bcebos.com. - Each item stored in BOS is required to reside within a bucket.
- Users can create up to 100 buckets, with no limits on the number or total size of objects stored in each bucket. Data scalability is automatically managed by the system, so users need not worry about capacity limits.
Bucket permission management
Set bucket access permissions
The following example sets the bucket's permission to private.
1public void setBucketPrivate (BosClient client, String bucketName) {
2 client.setBucketAcl(<bucketName>, CannedAccessControlList.Private);
3}
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 CannedAcl).
Set access permissions for a specific user on the bucket
BOS can also set the access permissions of a specified user to the bucket. Refer to the following code for implementation:
1List<Grant> accessControlList = new ArrayList<Grant>();
2List<Grantee> grantees = new ArrayList<Grantee>();
3List<Permission> permissions = new ArrayList<Permission>();
4List<String> ipAddress = new ArrayList<String>();
5List<String> stringLike = new ArrayList<String>();
6List<String> stringEquals = new ArrayList<String>();
7List<String> resource = new ArrayList<String>();
8List<String> notResource = new ArrayList<String>();
9Referer referer = new Referer();
10Condition condition = new Condition();
11 // Grant permission to specific user
12grantees.add(new Grantee("user_id1"));
13grantees.add(new Grantee("user_id2"));
14grantees.add(new Grantee("user_id3"));
15 //Grant permission to Everyone
16grantees.add(new Grantee("*"));
17 //Set permissions
18permissions.add(Permission.WRITE);
19permissions.add(Permission.READ);
20permissions.add(Permission.LIST);
21 // Set ip
22ipAddress.add("ipAddress1");
23ipAddress.add("ipAddress2");
24ipAddress.add("ipAddress3");
25condition.setIpAddress(ipAddress);
26 // Set refer stringLike
27stringLike.add("http://www.example1.com/");
28stringLike.add("http://www.example2.com/");
29stringLike.add("http://www.example3.com/");
30referer.setStringLike(stringLike);
31condition.setReferer(referer);
32 // Set refer stringEquals
33stringEquals.add("http://www.baidu.com");
34stringEquals.add("http://www.xiaomi.com");
35stringEquals.add("http://www.google.com");
36referer.setStringEquals(stringEquals);
37condition.setReferer(referer);
38 // Set resource
39resource.add("yourBucketName");
40 // Set notResource
41List<String> notResouce = new ArrayList<String>();
42notResouce.add("yourBucketName");
43notResouce.add("yourBucketName/*");
44Grant grant = new Grant();
45grant.setGrantee(grantees);
46grant.setPermission(permissions);
47grant.setCondition(condition);
48grant.setResource(resource);
49List<Grantee> grantees1 = new ArrayList<Grantee>();
50List<Permission> permissions1 = new ArrayList<Permission>();
51List<String> ipAddress1 = new ArrayList<String>();
52List<String> stringLike1 = new ArrayList<String>();
53List<String> stringEquals1 = new ArrayList<String>();
54List<String> resource1 = new ArrayList<String>();
55List<String> notResource1 = new ArrayList<String>();
56Referer referer1 = new Referer();
57Condition condition1 = new Condition();
58 // Grant permission to specific user
59grantees1.add(new Grantee("user_id4"));
60grantees1.add(new Grantee("user_id5"));
61grantees1.add(new Grantee("user_id6"));
62 //Grant permission to Everyone
63grantees.add(new Grantee("*"));
64 //Set permissions
65permissions.add(Permission.FULL_CONTROL);
66permissions1.add(Permission.WRITE);
67permissions1.add(Permission.READ);
68permissions1.add(Permission.LIST);
69 // Set ip
70ipAddress1.add("ipAddress4");
71ipAddress1.add("ipAddress5");
72ipAddress1.add("ipAddress6");
73condition1.setIpAddress(ipAddress1);
74 // Set refer stringLike
75stringLike1.add("http://www.example4.com/");
76stringLike1.add("http://www.example5.com/");
77stringLike1.add("http://www.example6.com/");
78referer1.setStringLike(stringLike1);
79condition1.setReferer(referer1);
80 // Set refer stringEquals
81stringEquals1.add("http://www.baidu1.com");
82stringEquals1.add("http://www.xiaomi1.com");
83stringEquals1.add("http://www.google1.com");
84referer1.setStringEquals(stringEquals1);
85condition1.setReferer(referer1);
86 // Set resource
87resource1.add("yourBucketName");
88 // Set notResource
89List<String> notResouce = new ArrayList<String>();
90notResouce.add("yourBucketName");
91notResouce.add("yourBucketName/*");
92Grant grant1 = new Grant();
93grant1.setGrantee(grantees1);
94grant1.setPermission(permissions1);
95grant1.setCondition(condition1);
96grant1.setResource(resource1);
97accessControlList.add(grant);
98accessControlList.add(grant1);
99SetBucketAclRequest request = new SetBucketAclRequest("yourBucketName",accessControlList);
100client.setBucketAcl(request);
Note: resource and notResource cannot be set at the same time The permission settings in Permission include three values:
READ,WRITE,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).
Set more bucket access permissions
- Set anti-leech through refer allow list
1String jsonAcl = "";
2client.setBucketAcl("bucketName", jsonAcl)
Among them, jsonAcl is {"accessControlList":["+ "{"grantee":[{"id":"*"}], "+ ""permission":["FULL_CONTROL"], "+ ""condition":{"referer":{"stringEquals":["http://test/index"]}" + "}}]}
- Restrict client IP access, only allow some client IPs to access
1String jsonAcl = "";
2client.setBucketAcl("bucketName", jsonAcl)
Among them, jsonAcl is {\"accessControlList\":["+ "{\"grantee\":[{\"id\":\"*\"}], "+ "\"permission\":[\"FULL_CONTROL\"], "+ "\"condition\":{\"ipAddress\":[\"192.170.0.6\"]" + "}}]}")
Set STS temporary token permissions
For temporary access identities created through STS, administrators can also set specific permissions. For an introduction to STS and how to set temporary permissions, please refer to [Temporary Authorization Access](BOS/API Reference/Access control.md#Temporary authorized access).
For setting STS temporary token permissions using the BOS JAVA SDK, you can refer to [Creating BosClient with STS](BOS/SDK/Java-SDK/Initialization.md#Create a BosClient with STS)
View bucket permissions
The following code can be used to view the bucket permissions:
1GetBucketAclResponse aclResponse = client.getBucketAcl("bucketName");
2System.out.println(aclResponse.getAccessControlList().toString());
The parameters available for calling in the resolution class returned by the getBucketAcl method are as follows:
| Parameters | Description |
|---|---|
| owner | Bucket owner information |
| id | User ID of bucket owner |
| acl | Identify the permission list of the bucket |
| grantee | Identify the grantee |
| -id | Authorized person ID |
| permission | Identify the grantee permissions |
View the region to which a bucket belongs
Bucket Location refers to Bucket Region. For details on regions supported by Baidu AI Cloud, refer to [Region Selection Guide](Reference/Region Selection Instructions/Region.md).
The following code can retrieve the bucket's location information:
1BosClient client = new BosClient(config);
2ListBucketsResponse listBucketsResponse = client.listBuckets();
3List<BucketSummary> bucketSummaryList = listBucketsResponse.getBuckets();
4for(BucketSummary bs : bucketSummaryList){
5 System.out.println( bs.getLocation());
6}
7System.out.println(client.getBucketLocation("bucket-test").getLocationConstraint());
Create Bucket
The following code can be used to create a bucket:
1public void createBucket (BosClient client, String bucketName) {
2 // Create a new bucket
3 client.createBucket(<bucketName>); //Specify bucket name
4}
Note: Since the bucket name is unique across all regions, it is necessary to ensure that the bucketName is not the same as the BucketName on all other regions.
Bucket naming follows the following conventions:
- Only lowercase letters, numbers, and hyphens (-) are allowed.
- Must start with a lowercase letter or a number.
- Length should range between 4-63 bytes.
The bucket created using the above code has private read-write permissions and a standard (Standard) storage class.
- Create a new lcc bucket
The Java SDK supports creating LCC buckets. Users can fill in lcclocation in the request for creating a new bucket. The specific parameters of CreateBucketRequest are as follows:
| Parameters | Types | Description |
|---|---|---|
| bucketTags | String | Bucket tags |
| lccLocation | String | lcc id, used to create an lcc bucket in a specific lcc cluster |
| enableDedicated | Boolean | Used to control the console to enable the lcc recognition allow list |
The following code can be used to create a lcc bucket:
1public void createBucket (BosClient client, String bucketName) {
2 CreateBucketRequest request = new CreateBucketRequest("bucketName");
3 // Set lcc location id
4 request.setLccLocation("lcc id");
5 request.setEnableDedicated(true);
6 client.createBucket(request);
7}
List buckets
List all the user’s buckets using the following code or with reference to Complete Example.
1public void listBuckets (BosClient client) {
2 // Obtain the user's bucket list
3 List<BucketSummary> buckets = client.listBuckets().getBuckets();
4 // Traverse bucket
5 for (BucketSummary bucket : buckets) {
6 System.out.println(bucket.getName());
7 }
8}
Delete bucket
Delete a bucket using the following code or with reference to Complete Example.
1public void deleteBucket (BosClient client, String bucketName) {
2 // Delete bucket
3 client.deleteBucket(<BucketName>); //Specify bucket name
4}
Note:
- Before deletion, ensure that all objects and any unfinished multipart uploads in the bucket have been fully removed. Otherwise, the deletion will fail.
- Before deleting a bucket, verify that Cross-Region Replication (CRR) is not enabled for the bucket, and that it is neither the source nor target bucket in any CRR rules. Otherwise, the deletion will fail.
Check if bucket exists
Users can determine whether a bucket exists using the following code or with reference to Complete Example.
1public void doesBucketExist (BosClient client, String bucketName) {
2 // Retrieve bucket existence information
3 boolean exists = client.doesBucketExist(<BucketName>); //Specify the bucket name
4 // Output result
5 if (exists) {
6 System.out.println("Bucket exists");
7 } else {
8 System.out.println("Bucket not exists");
9 }
10}
Note: If the bucket is not null (that is, there are objects in the bucket), the bucket cannot be deleted. The bucket must be emptied before it can be deleted successfully.
Set bucket storage class
A newly created bucket is of the standard storage class by default. Users can set or get the storage class of a specified bucket through the following code:
| Parameters | Description |
|---|---|
| storageClass | Storage classes, supporting "STANDARD", "STANDARD_IA", "COLD", "ARCHIVE" |
1public void PutBucketStorageClass() {
2 // Set to "STANDARD_IA", infrequent access storage
3 String storageClass = STORAGE_CLASS_STANDARD_IA;
4 this.client.putBucketStorageClass(this.bucketName, storageClass);
5}
6public String GetBucketStorageClass() {
7 GetBucketStorageClassResponse response = this.client.getBucketStorageClass(this.bucketName);
8 return response.getStorageClass();
9}
Set Bucket server-side encryption
If users need to enable the server-side encryption of the bucket, the following code can achieve this:
| Parameters | Description |
|---|---|
| encryptionAlgorithm | This specifies the server-side encryption type for a bucket, which currently supports AES256 encryption only. |
1public void PutBucketEncryptionByEncryption(BosClient client, String bucketName, String encryptionAlgorithm ) {
2 SetBucketEncryptionRequest setBucketEncryptionRequest = new SetBucketEncryptionRequest();
3 setBucketEncryptionRequest.setBucketName(bucketName);
4 BucketEncryption encryption = new BucketEncryption();
5 encryption.setEncryptionAlgorithm(encryptionAlgorithm);
6 setBucketEncryptionRequest.setBucketEncryption(encryption);
7 client.setBucketEncryption(setBucketEncryptionRequest);
8}
If users want to view the server-side encryption information of the bucket, the following code can achieve this:
1public GetBucketEncryptionResponse GetBucketEncryption(BosClient client, String bucketName) {
2 GetBucketEncryptionRequest getBucketEncryptionRequest = new GetBucketEncryptionRequest();
3 getBucketEncryptionRequest.withBucketName(bucketName);
4 GetBucketEncryptionResponse resp = new GetBucketEncryptionResponse();
5 resp = client.getBucketEncryption(getBucketEncryptionRequest);
6 return resp;
7}
If users want to delete the server-side encryption information of the bucket, the following code can achieve this:
1public void DeleteBucketEncryption(BosClient client, String bucketName) {
2 DeleteBucketEncryptionRequest deleteBucketEncryptionRequest = new DeleteBucketEncryptionRequest();
3 deleteBucketEncryptionRequest.withBucketName(bucketName);
4 client.deleteBucketEncryption(deleteBucketEncryptionRequest);
5}
Bucket data synchronization
If users need to enable data synchronization between buckets, the following code can achieve this:
| Parameters | Required or not | Description |
|---|---|---|
| id | Yes | Rule name of replication. The ID must consist of numbers, letters, hyphens (-), and underscores (_), and shall not exceed 20 characters. |
| status | Yes | Whether it takes effect; “enabled” means it is effective |
| resource | Yes | Effective prefix of replication. The resource configuration format is {$bucket_name/$bucket_name+/. |
| destination | Yes | Destination location configuration of replication |
| +bucket | Yes | Destination Bucket name |
| +storageClass | No | Storage class of destination object. If you want to keep the same storage class as the source Bucket, this parameter does not need to be configured; if you need to specify a separate storage class, it can be STANDARD, STANDARD_IA, COLD. |
| replicateHistory | No | Historical file replication: When enabled, all existing objects will be synchronously replicated to the destination bucket, sharing the same resource scope. |
| +storageClass | No | Storage class of destination object. If you want to keep the same storage class as the source Bucket, this parameter does not need to be configured; if you need to specify a separate storage class, it can be STANDARD,STANDARD_IA and COLD. |
| replicateDeletes | Yes | It indicates whether Delete synchronization function is enabled. It is enabled,disabled. |
1public void PutBucketReplicationByReplication(BosClient client, String bucketName, String replicationId, String dstBucketName) {
2 SetBucketReplicationRequest request = new SetBucketReplicationRequest(bucketName);
3 request.setId(replicationId);
4 request.setStatus("enabled");
5 String[] resource = {bucketName + "/abc"};
6 request.setResource(resource);
7 Destination destination = new Destination();
8 destination.setBucket(dstBucketName);
9 request.setDestination(destination);
10 request.setReplicateDeletes("enabled");
11 client.setBucketReplication(request);
12 }
Users can obtain data synchronization details for a specified bucket ID, including the source bucket name, destination bucket name, storage class, whether historical replication is enabled, data synchronization policy, destination region, and more. The following code demonstrates this:
1public void GetBucketReplication(BosClient client, String bucketName, String replicationId) {
2 GetBucketReplicationRequest grequest = new GetBucketReplicationRequest(bucketName);
3 grequest.setId(replicationId);
4 GetBucketReplicationResponse response = client.getBucketReplication(grequest);
5 }
If users want to delete the data synchronization with a specified ID in the bucket, the following code can achieve this:
1public void DeleteBucketReplication(BosClient client, String bucketName, String replicationId) {
2 DeleteBucketReplicationRequest drequest = new DeleteBucketReplicationRequest();
3 drequest.setBucketName(this.bucketName);
4 drequest.setId(this.replicationId);
5 client.deleteBucketReplication(drequest);
6}
If users want to get all replication synchronization rules of the bucket, the following code can achieve this:
1public void ListBucketReplication(BosClient client, String bucketName) {
2 ListBucketReplicationResponse replicationResponse;
3 ListBucketReplicationRequest listreq = new ListBucketReplicationRequest(this.bucketName);
4 replicationResponse = client.listBucketReplication(listreq);
5}
If users want to get the progress status of data synchronization replication with a specified ID, the following code can achieve this:
1public void GetBucketReplicationProgress(BosClient client, String bucketName,String replicationId) {
2 GetBucketReplicationProgressRequest proreq = new GetBucketReplicationProgressRequest(this.bucketName);
3 proreq.setId(this.replicationId);
4 BucketReplicationProgress progress = client.getBucketReplicationProgress(proreq);
5}
Bucket static website hosting
Hosting a website on a bucket enables lightweight operation and maintenance, which can be achieved with the following code:
| Parameters | Description |
|---|---|
| index | Index file name |
| notFound | 404 file name |
1public void PutBucketStaticWebsite(BosClient client, String bucketName, String index, String notFound) {
2 SetBucketStaticWebsiteRequest setBucketStaticWebsiteRequest = new SetBucketStaticWebsiteRequest();
3 setBucketStaticWebsiteRequest.setBucketName(bucketName);
4 setBucketStaticWebsiteRequest.setIndex(index);
5 setBucketStaticWebsiteRequest.setNotFound(notFound);
6 client.setBucketStaticWebSite(setBucketStaticWebsiteRequest);
7}
If users want to view the static website hosting information, the following code can achieve this:
1public GetBucketStaticWebsiteResponse GetBucketStaticWebsite(BosClient client, String bucketName) {
2 GetBucketStaticWebsiteRequest getBucketStaticWebsiteRequest = new GetBucketStaticWebsiteRequest();
3 getBucketStaticWebsiteRequest.withBucketName(bucketName);
4 GetBucketStaticWebsiteResponse resp = new GetBucketStaticWebsiteResponse();
5 resp = client.getBucketStaticWebsite(getBucketStaticWebsiteRequest);
6 return resp;
7}
Users can disable the static website hosting function using the following code:
1public void DeleteBucketStaticWebsite(BosClient client, String bucketName) {
2 DeleteBucketStaticWebsiteRequest deleteBucketStaticWebsiteRequest = new DeleteBucketStaticWebsiteRequest();
3 deleteBucketStaticWebsiteRequest.withBucketName(bucketName);
4 client.deleteBucketStaticWebSite(deleteBucketStaticWebsiteRequest);
5}
Original image protection
If users want to enable the original image protection function of the bucket, the following code can achieve this:
| Parameters | Description |
|---|---|
| resource | Indicate the scope of effective resources |
1public void PutBucketCopyrightProtection(BosClient client, String bucketName, List<String> resource) {
2 SetBucketCopyrightProtectionRequest request = new SetBucketCopyrightProtectionRequest();
3 request.setBucketName(bucketName);
4 request.setResource(resource);
5 client.setBucketCopyrightProtection(request);
6}
If users want to get the original image protection configuration details of the bucket, the following code can achieve this:
1public GetBucketCopyrightProtectionResponse GetBucketCopyrightProtection(BosClient client, String bucketName) {
2 GetBucketCopyrightProtectionRequest getBucketCopyrightProtectionRequest =
3 new GetBucketCopyrightProtectionRequest();
4 getBucketCopyrightProtectionRequest.withBucketName(bucketName);
5 GetBucketCopyrightProtectionResponse resp = new GetBucketCopyrightProtectionResponse();
6 resp = client.getBucketCopyrightProtection(getBucketCopyrightProtectionRequest);
7 return resp;
8}
If users want to disable the original image protection function of the bucket, the following code can achieve this:
1public void DeleteBucketCopyrightProtection(BosClient client, String bucketName) {
2 DeleteBucketCopyrightProtectionRequest deleteBucketCopyrightProtectionRequest =
3 new DeleteBucketCopyrightProtectionRequest();
4 deleteBucketCopyrightProtectionRequest.withBucketName(bucketName);
5 client.deleteBucketCopyrightProtection(deleteBucketCopyrightProtectionRequest);
6}
