Baidu AI Cloud
中国站

百度智能云

Object Storage

Bucket Management

Create Bucket

  • Basic procedure

    1.Create an instance of BOSClient class 2.Call BOSClient.Createbucket() method. You need to provide the name of bucket, namely <bucketname> .

  • Sample Code

      public void CreateBucket(BosClient client, string bucketName)
      {
        // Create a bucket
        client.CreateBucket(bucketName); 
      }                             

    Note: The name of bucket is unique in all regions, so you need to guarantee that BucketName is not the same with the name of bucket in all other regions.

  • Complete example

    See complete example.

View Bucket List

  • Basic procedure

    1.Create an instance of BOSClient class 2.Call BOSClient.ListBuckets() method to obtain bucket list. 3.Traverse bucket list, and view relevant information.

  • Sample Code

      public void ListBuckets(BosClient client) 
      { 
      
        // Get the user's bucket list. 
        List<BucketSummary> buckets = client.ListBuckets().buckets; 
      
        // Traverse all buckets. 
        foreach (BucketSummary bucket in buckets) 
        { 
          Console.WriteLine(bucket.Name); 
        } 
      
      } 

    See complete example.

Judge whether a Bucket Exists or Not

  • Basic procedure

    1.Create an instance of BOSClient class 2.Call BOSClient.DoesBucketExist() method to obtain the result.

  • Sample Code

    To judge whether a bucket exists, you need to do with the following code:

      public void DoesBucketExist(BosClient client, string bucketName) 
      { 
      
        // Obtain the presence information of the bucket 
        bool exists = client.DoesBucketExist(bucketName); //Designate the name of the bucket: 
      
        // Output result 
        if (exists) 
        { 
          Console.WriteLine("Bucket exists"); 
        } 
        else 
        { 
          Console.WriteLine("Bucket not exists"); 
        } 
      } 
  • Complete example

    See complete example.

Delete Bucket

  • Basic procedure

    1.Create an instance of BOSClient class 2.Call BOSClient.DeleteBucket() method to delete bucket.

  • Sample Code

      public void DeleteBucket(BosClient client, string BucketName) 
      { 
      
        // Delete bucket 
        client.DeleteBucket(bucketName); 
      } 

    Note: If the bucket is not null (i.e. bucket has object and unfinished three-step upload Parts), the bucket cannot be deleted and must be emptied to be deleted successfully.

  • Complete example

    See complete example.

Bucket Privilege Control

  • Basic procedure

    1.Create an instance of BOSClient class 2.Call BOSClient.SetBucketAcl() method. When calling this method, you need to provide bucket name, privilege string, etc. 3.You can also create a SetBucketAclRequest to call SetBucketAcl method, and SetBucketAclRequest can set access privilege of specified users to bucket.

Set access privileges of bucket

  • Sample Code

    The following code sets the privileges of bucket private.

      public void SetbucketPrivate(BosClient client, string bucketName) 
      { 
        client.SetBucketAcl(bucketName, BosConstants.CannedAcl.Private); 
      } 

    Note: BosConstants.CannedAcl contains three constant definitions: Private, PublicRead, PublicReadWrite . They correspond to the respective privileges. For specific contents, please see BOS API Document privilege Control Through CannedAcl.

Set the specified user's access to the bucket

  • Sample Code

    BOS can also set the access privilege of specified users to bucket, see the following codes:

      public void SetBucketAclFromBody(BosClient client, string bucketName) 
      { 
        List<Grant> grants = new List<Grant>(); 
        List<Grantee> grantee = new List<Grantee>(); 
        List<string> privilege = new List<string>(); 
      
        //privilege to specific users 
        grantee.Add(new Grantee() {Id = "userid1"}); 
        grantee.Add(new Grantee() {Id = "userid2"}); 
                
        //privilege to Everyone 
        grantee.Add(new Grantee() {Id = "*"}); 
      
        //privilege setting 
        privilege.Add(BosConstants.privilege.Read); 
        privilege.Add(BosConstants.privilege.Write); 
      
        grants.Add(new Grant() {Grantee = grantee, privilege = privilege}); 
        client.SetBucketAcl(new SetBucketAclRequest() {BucketName = bucketName, AccessControlList = grants}); 
      } 

    Note: BosConstants.privilege contains three constant definitions: READ, WRITE and FULL_CONTROL, corresponding to respective privileges. For the specific contents, please see BOS API Document Privilege Control through ACL File Uploading.

Complete example

The following sample code demonstrates the complete process of bucket privilege control:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using BaiduBce; 
using BaiduBce.Auth; 
using BaiduBce.Services.Bos; 
using BaiduBce.Services.Bos.Model; 

namespace DotnetSample 
{ 
    internal class BucketAclSample 
    { 
        private static void Main(string[] args) 
        { 
            BosClient client = GenerateBosClient(); 
            const string bucketName =<BucketName>; //Bucket name 

            // Create a bucket. 
            client.Createbucket(bucketName); //Designate the name of the bucket: 


            // Set Bucket as Private. 
            client.SetBucketAcl(bucketName, BosConstants.CannedAcl.Private); 

            // Set Bucket as PublicRead. 
            client.SetBucketAcl(BucketName, BosConstants.CannedAcl.PublicRead); 

            List<Grant> grants = new List<Grant>(); 
            List<Grantee> grantee = new List<Grantee>(); 
            List<string> privilege = new List<string>(); 

            //privilege to specific users 
            grantee.Add(new Grantee() { Id = "userid1" }); 
            grantee.Add(new Grantee() { Id = "userid2" }); 
            //privilege to Everyone 
            grantee.Add(new Grantee() { Id = "*" }); 

            //privilege setting 
            privilege.Add(BosConstants.privilege.Read); 
            privilege.Add(BosConstants.privilege.Write); 

            grants.Add(new Grant() { Grantee = grantee, privilege = privilege }); 
            client.SetBucketAcl(new SetBucketAclRequest() { BucketName = bucketName, AccessControlList = grants }); 
            
        } 

        private static BosClient GenerateBosClient() 
        { 
            const string accessKeyId =<AccessKeyID>;        // Your Access Key ID 
            const string secretAccessKey =<SecretAccessKey>; // Your Secret Access Key 
            const string endpoint =<EndPoint>;       // Specify BOS service domain name 

            // Initialize a BosClient 
            BceClientConfiguration config = new BceClientConfiguration(); 
            config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey); 
            config.Endpoint = endpoint; 

            return new BosClient(config); 
        } 
    } 
} 

Complete Example

The following sample code demonstrates the complete process of creating bucket, viewing bucket, judging whether bucket exists and deleting bucket.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using BaiduBce; 
using BaiduBce.Auth; 
using BaiduBce.Services.Bos; 
using BaiduBce.Services.Bos.Model; 

namespace DotnetSample 
{ 
    internal class BucketSample 
    { 
        private static void Main(string[] args) 
        { 
            BosClient client = GenerateBosClient(); 
            const string bucketName =<BucketName>;    //Designate the name of the bucket: 

            // Create a bucket. 
            client.Createbucket(bucketName); 

            // Get the user's bucket list. 
            List<BucketSummary> buckets = client.ListBuckets().Buckets; 
            
            // Traverse all Buckets. 
            foreach (BucketSummary bucket in buckets) 
            { 
                Console.WriteLine(bucket.Name); 
            } 

            // Obtain the presence information of the bucket 
            bool exists = client.DoesBucketExist(BucketName); 
            
            // Output result 
            if (exists) 
            { 
                Console.WriteLine("Bucket exists"); 
            } 
            else 
            { 
                Console.WriteLine("Bucket not exists"); 
            } 

            // Delete Bucket 
            client.DeleteBucket(BucketName); 

            // Obtain the presence information of the bucket 
            exists = client.DoesBucketExist(bucketName); 
            
            // Output result 
            if (exists) 
            { 
                Console.WriteLine("Bucket exists"); 
            } 
            else 
            { 
                Console.WriteLine("Bucket not exists"); 
            } 
            Console.ReadKey(); 
        } 

        private static BosClient GenerateBosClient() 
        { 
            const string accessKeyId =<AccessKeyID>; // Your Access Key ID 
            const string secretAccessKey =<SecretAccessKey>; // Your Secret Access Key 
            const string endpoint =<EndPoint>; // Specify BOS service domain name 

            // Initialize a BosClient 
            BceClientConfiguration config = new BceClientConfiguration(); 
            config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey); 
            config.Endpoint = endpoint; 

            return new BosClient(config); 
        } 
    } 
}
Previous
SDK Installation
Next
Object Management