edit-icon download-icon

Manage buckets

Last Updated: Nov 07, 2017

A bucket is a namespace on the OSS and a management entity for billing, access control, logging, and other advanced features.

Create a bucket

The following code creates a bucket:

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. /// <summary>
  5. /// Create a new bucket
  6. /// </summary>
  7. /// <param name="bucketName">Name of the bucket</param>
  8. public void CreateBucket(string bucketName)
  9. {
  10. try
  11. {
  12. // Create a bucket
  13. client.CreateBucket(bucketName);
  14. Console.WriteLine("Create bucket succeeded");
  15. }
  16. catch (Exception ex)
  17. {
  18. Console.WriteLine("Create bucket failed. {0}", ex.Message);
  19. }
  20. }

For complete code, see GitHub.

Note: The name of a bucket is globally unique. Hence, you must make sure that your bucket name is different from those of other users.

List all buckets of a user

The following code lists all buckets of a specified user:

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. /// <summary>
  5. /// List the information of all buckets under an account
  6. /// </summary>
  7. public void ListBuckets()
  8. {
  9. try
  10. {
  11. var buckets = client.ListBuckets();
  12. Console.WriteLine("List bucket succeeded");
  13. foreach (var bucket in buckets)
  14. {
  15. Console.WriteLine("Bucket name: {0}, Location: {1}, Owner: {2}", bucket.Name, bucket.Location, bucket.Owner);
  16. }
  17. }
  18. catch (Exception ex)
  19. {
  20. Console.WriteLine("List bucket failed. {0}", ex.Message);
  21. }
  22. }

For complete code, see GitHub.

CNAME access

Once you direct your domain CNAME to the domain of your bucket, you can access OSS through your domain:

To use the CNAME for access, you must set IsCname in ClientConfigration to true.

  1. using Aliyun.OSS;
  2. using Aliyun.OSS.Common;
  3. /// <summary>
  4. /// Upload objects using the CNAME
  5. /// </summary>
  6. public void PutObjectByCname()
  7. {
  8. try
  9. {
  10. // For example, your domain is "http://my-cname.com" and you can direct the CNAME to your bucket domain "mybucket.oss-cn-hangzhou.aliyuncs.com"
  11. // Create a ClientConfiguration instance
  12. var conf = new ClientConfiguration();
  13. // Use CNAME for configuration
  14. conf.IsCname = true;
  15. var client = new OssClient("http://my-cname.com/", accessKeyId, accessKeySecret, conf);
  16. var result = client.putObject("mybucket", key, fileToUpload);
  17. Console.WriteLine("Put object succeeded");
  18. }
  19. catch (Exception ex)
  20. {
  21. Console.WriteLine("Put object failed. {0}", ex.Message);
  22. }
  23. }

For complete code, see GitHub

Note:

  • When you create an OSSClient class instance, replace the endpoint for the bucket with the domain name mapped to the CNAME and set IsCname in ClientConfiguration to true.

  • When you use the OSSClient instance for subsequent operations, the bucket name can only be set to the name to which the CNAME is directed.

Identify whether the bucket exists

The following code checks whether a bucket exists:

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. /// <summary>
  5. /// Identify whether the bucket exists
  6. /// </summary>
  7. /// <param name="bucketName">Name of the bucket</param>
  8. public void DoesBucketExist(string bucketName)
  9. {
  10. try
  11. {
  12. var exist = client.DoesBucketExist(bucketName);
  13. Console.WriteLine("Check object Exist succeeded");
  14. Console.WriteLine("exist ? {0}", exist);
  15. }
  16. catch (Exception ex)
  17. {
  18. Console.WriteLine("Check object Exist failed. {0}", ex.Message);
  19. }
  20. }

For complete code, see GitHub.

Configure the access ACL of a bucket

The following code configures the access ACL of a bucket:

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. /// <summary>
  5. /// Configures the access ACL of the bucket
  6. /// </summary>
  7. /// <param name="bucketName">Name of the bucket</param>
  8. public void SetBucketAcl(string buckteName)
  9. {
  10. try
  11. {
  12. // Make bucket ACL as public read
  13. client.SetBucketAcl(bucketName, CannedAccessControlList.PublicRead);
  14. Console.WriteLine("Set bucket ACL succeeded");
  15. }
  16. catch (Exception ex)
  17. {
  18. Console.WriteLine("Set bucket ACL failed. {0}", ex.Message);
  19. }
  20. }

For complete code, see GitHub.

Obtain the access ACL of a bucket

The following code obtains the access ACL of a bucket:

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. /// <summary>
  5. /// Obtain the access ACL of the bucket
  6. /// </summary>
  7. /// <param name="bucketName">Name of the bucket</param>
  8. public void GetBucketAcl(string bucketName)
  9. {
  10. try
  11. {
  12. string bucketName = "your-bucket";
  13. var acl = client.GetBucketAcl(bucketName);
  14. Console.WriteLine("Get bucket ACL success");
  15. foreach (var grant in acl.Grants)
  16. {
  17. Console.WriteLine ("The bucket ACL has been obtained successfully. Current permission: 0", grant.Permission.ToString());
  18. }
  19. }
  20. catch (Exception ex)
  21. {
  22. Console.WriteLine("Get bucket ACL failed. {0}", ex.Message);
  23. }
  24. }

For complete code, see GitHub.

Delete a bucket

The following code deletes a bucket:

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. /// <summary>
  5. /// Delete the bucket
  6. /// </summary>
  7. /// <param name="bucketName">Name of the bucket</param>
  8. public void DeleteBucket(string bucketName)
  9. {
  10. try
  11. {
  12. client.DeleteBucket(bucketName);
  13. Console.WriteLine("Delete bucket succeeded");
  14. }
  15. catch (Exception ex)
  16. {
  17. Console.WriteLine("Delete bucket failed. {0}", ex.Message);
  18. }
  19. }

For complete code, see GitHub.

Note: A non-empty bucket (which contains objects or object fragments from multipart uploads) cannot be deleted. To delete a non-empty bucket, you must first delete all objects in it.

Thank you! We've received your feedback.