Bucket management
Updated at:2025-11-03
Create Bucket
-
Basic workflow
- Create an instance of the BOSClient class.
- Call the BosClient.CreateBucket() method. You need to provide the name of the bucket, which is
<Bucketname>.
- Example code
C#
1public void CreateBucket(BosClient client, string bucketName)
2{
3 // Create a new bucket
4 client.CreateBucket(bucketName);
5}
Plain Text
1 > **Note:** Since bucket names are globally unique across all regions, ensure the BucketName does not conflict with any existing BucketName in other regions.
- Complete example
Plain Text
1Please refer to [complete example](#Complete example).
View bucket list
- Basic workflow
Plain Text
11. Create an instance of the BOSClient class.
22. Use the BosClient.ListBuckets() method to retrieve the list of buckets.
33. Iterate through the bucket list to view relevant details.
- Example code
C#
1public void ListBuckets(BosClient client)
2{
3 // Obtain the user's bucket list
4 List<BucketSummary> buckets = client.ListBuckets().Buckets;
5 // Traverse bucket
6 foreach (BucketSummary bucket in buckets)
7 {
8 Console.WriteLine(bucket.Name);
9 }
10}
Please refer to [complete example](#Complete example).
Check if bucket exists
- Basic workflow
Plain Text
11. Create an instance of the BOSClient class.
22. Execute the BosClient.DoesBucketExist() method to check for the existence of a bucket.
- Example code
Plain Text
1If users need to determine whether a bucket exists, the following code can achieve this:
C#
1public void DoesBucketExist(BosClient client, string bucketName)
2{
3 // Retrieve bucket existence information
4 bool exists = client.DoesBucketExist(bucketName);//Specify the bucket name
5 // Output result
6 if (exists)
7 {
8 Console.WriteLine("Bucket exists");
9 }
10 else
11 {
12 Console.WriteLine("Bucket not exists");
13 }
14}
- Complete example
Plain Text
1Please refer to [complete example](#Complete example).
Delete bucket
- Basic workflow
Plain Text
11. Create an instance of the BOSClient class.
22. Use the BosClient.DeleteBucket() method to delete a bucket.
- Example code
C#
1public void DeleteBucket(BosClient client, string bucketName)
2{
3 // Delete bucket
4 client.DeleteBucket(bucketName);
5}
Plain Text
1 > **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
Plain Text
1Please refer to [complete example](#Complete example).
Bucket permission control
- Basic workflow
Plain Text
11. Create an instance of the BOSClient class.
22. Run the BosClient.SetBucketAcl() method. When using this method, provide the bucket name, permission string, and other necessary details.
33. Alternatively, you can create a SetBucketAclRequest to call the SetBucketAcl method. SetBucketAclRequest allows you to configure the access permissions of a specific user for the bucket.
Set bucket access permission
- Example code
Plain Text
1The following example sets the bucket's permission to private.
C#
1public void SetBucketPrivate(BosClient client, string bucketName)
2{
3 client.SetBucketAcl(bucketName, BosConstants.CannedAcl.Private);
4}
Plain Text
1 > **Note: **BosConstants.CannedAcl contains three constant definitions: `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
- Example code
Plain Text
1BOS can also set the access permissions of a specified user to the bucket. Refer to the following code for implementation:
C#
1public void SetBucketAclFromBody(BosClient client, string bucketName)
2{
3 List<Grant> grants = new List<Grant>();
4 List<Grantee> grantee = new List<Grantee>();
5 List<string> permission = new List<string>();
6 //Grant permission to specific user
7 grantee.Add(new Grantee() {Id = "userid1"});
8 grantee.Add(new Grantee() {Id = "userid2"});
9 //Grant permission to Everyone
10 grantee.Add(new Grantee() {Id = "*"});
11 //Set permissions
12 permission.Add(BosConstants.Permission.Read);
13 permission.Add(BosConstants.Permission.Write);
14 grants.Add(new Grant() {Grantee = grantee, Permission = permission});
15 client.SetBucketAcl(new SetBucketAclRequest() {BucketName = bucketName, AccessControlList = grants});
16}
Plain Text
1 > **Note:**
2 > - BosConstants.Permission contains three constant definitions: `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).
3 > - The C# SDK does not support authentication through STS.
Complete example
The following sample code demonstrates the complete process of bucket permission control:
C#
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using BaiduBce;
6using BaiduBce.Auth;
7using BaiduBce.Services.Bos;
8using BaiduBce.Services.Bos.Model;
9namespace DotnetSample
10{
11 internal class BucketAclSample
12 {
13 private static void Main(string[] args)
14 {
15 BosClient client = GenerateBosClient();
16 const string bucketName = <BucketName>;//Bucket name
17 // Create a new bucket
18 client.CreateBucket(bucketName); //Specify bucket name
19 //Set the bucket to private
20 client.SetBucketAcl(bucketName, BosConstants.CannedAcl.Private);
21 //Set the bucket to PublicRead
22 client.SetBucketAcl(bucketName, BosConstants.CannedAcl.PublicRead);
23 List<Grant> grants = new List<Grant>();
24 List<Grantee> grantee = new List<Grantee>();
25 List<string> permission = new List<string>();
26 //Grant permission to specific user
27 grantee.Add(new Grantee() { Id = "userid1" });
28 grantee.Add(new Grantee() { Id = "userid2" });
29 //Grant permission to Everyone
30 grantee.Add(new Grantee() { Id = "*" });
31 //Set permissions
32 permission.Add(BosConstants.Permission.Read);
33 permission.Add(BosConstants.Permission.Write);
34 grants.Add(new Grant() { Grantee = grantee, Permission = permission });
35 client.SetBucketAcl(new SetBucketAclRequest() { BucketName = bucketName, AccessControlList = grants });
36 }
37 private static BosClient GenerateBosClient()
38 {
39 const string accessKeyId = <AccessKeyID>;//Your Access Key ID
40 const string secretAccessKey = <SecretAccessKey>; //Your Secret Access Key
41 const string endpoint = "https://bj.bcebos.com";//Specify the BOS service domain name
42 // Initialize a BOSClient
43 BceClientConfiguration config = new BceClientConfiguration();
44 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
45 config.Endpoint = endpoint;
46 return new BosClient(config);
47 }
48 }
49}
Complete example
The example code below illustrates the full process of creating a bucket, viewing its details, checking its existence, and deleting it.
C#
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using BaiduBce;
6using BaiduBce.Auth;
7using BaiduBce.Services.Bos;
8using BaiduBce.Services.Bos.Model;
9namespace DotnetSample
10{
11 internal class BucketSample
12 {
13 private static void Main(string[] args)
14 {
15 BosClient client = GenerateBosClient();
16 const string bucketName = <BucketName>; //Specify bucket name
17 // Create a new bucket
18 client.CreateBucket(bucketName);
19 // Obtain the user's bucket list
20 List<BucketSummary> buckets = client.ListBuckets().Buckets;
21 // Traverse bucket
22 foreach (BucketSummary bucket in buckets)
23 {
24 Console.WriteLine(bucket.Name);
25 }
26 // Retrieve bucket existence information
27 bool exists = client.DoesBucketExist(bucketName);
28 // Output result
29 if (exists)
30 {
31 Console.WriteLine("Bucket exists");
32 }
33 else
34 {
35 Console.WriteLine("Bucket not exists");
36 }
37 // Delete bucket
38 client.DeleteBucket(bucketName);
39 // Retrieve bucket existence information again
40 exists = client.DoesBucketExist(bucketName);
41 // Output result
42 if (exists)
43 {
44 Console.WriteLine("Bucket exists");
45 }
46 else
47 {
48 Console.WriteLine("Bucket not exists");
49 }
50 Console.ReadKey();
51 }
52 private static BosClient GenerateBosClient()
53 {
54 const string accessKeyId = <AccessKeyID>;//Your Access Key ID
55 const string secretAccessKey = <SecretAccessKey>; //Your Secret Access Key
56 const string endpoint = "https://bj.bcebos.com";//Specify the BOS service domain name
57 // Initialize a BOSClient
58 BceClientConfiguration config = new BceClientConfiguration();
59 config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
60 config.Endpoint = endpoint;
61 return new BosClient(config);
62 }
63 }
64}
