存储桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器储存对象(Object)数据。新创建的桶名在 TOS 中必须是唯一的。本文介绍如何创建桶。
以下代码用于在创建存储桶时指定 ACL。
using System; using TOS; using TOS.Error; using TOS.Model; namespace ConsoleApp { internal class Program { private static void Main(string[] args) { var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY"); var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY"); // endpoint 若没有指定HTTP协议(HTTP/HTTPS),默认使用 HTTPS // Bucket 的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com var endpoint = "https://tos-cn-beijing.volces.com"; var region = "cn-beijing"; // 填写 BucketName var bucketName = "*** Provide your bucket name ***"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { // 创建存储空间输入 var createBucketInput = new CreateBucketInput() { Bucket = bucketName, // 指定存储权限类型为私有 ACL = ACLType.ACLPrivate }; // 创建存储空间。 var createBucketOutput = client.CreateBucket(createBucketInput); Console.WriteLine("Create bucket succeeded, request id {0} ", createBucketOutput.RequestID); Console.WriteLine("Create bucket succeeded, status code {0} ", createBucketOutput.StatusCode); } catch (TosServerException ex) { Console.WriteLine("Create bucket failed, request id {0}", ex.RequestID); Console.WriteLine("Create bucket failed, status code {0}", ex.StatusCode); Console.WriteLine("Create bucket failed, response error code {0}", ex.Code); Console.WriteLine("Create bucket failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("Create bucket failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("Create bucket failed, {0}", ex.Message); } } } }
以下代码用于在创建存储桶时指定存储类型。
using System; using TOS; using TOS.Error; using TOS.Model; var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY"); var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY"); // endpoint 若没有指定HTTP协议(HTTP/HTTPS),默认使用 HTTPS // Bucket 的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com var endpoint = "https://tos-cn-beijing.volces.com"; var region = "cn-beijing"; // 填写 BucketName var bucketName = "*** Provide your bucket name ***"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { // 创建存储空间输入 var createBucketInput = new CreateBucketInput() { Bucket = bucketName, // 指定存储桶存储类型为低频存储 StorageClass = StorageClassType.StorageClassIa }; // 创建存储空间。 var createBucketOutput = client.CreateBucket(createBucketInput); Console.WriteLine("Create bucket succeeded, request id {0} ", createBucketOutput.RequestID); Console.WriteLine("Create bucket succeeded, status code {0} ", createBucketOutput.StatusCode); } catch (TosServerException ex) { Console.WriteLine("Create bucket failed, request id {0}", ex.RequestID); Console.WriteLine("Create bucket failed, status code {0}", ex.StatusCode); Console.WriteLine("Create bucket failed, response error code {0}", ex.Code); Console.WriteLine("Create bucket failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("Create bucket failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("Create bucket failed, {0}", ex.Message); }
以下代码展示用于在创建存储桶时指定 AZ 类型。
using System; using TOS; using TOS.Error; using TOS.Model; namespace ConsoleApp { internal class Program { private static void Main(string[] args) { var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY"); var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY"); // endpoint 若没有指定HTTP协议(HTTP/HTTPS),默认使用 HTTPS // Bucket 的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com var endpoint = "https://tos-cn-beijing.volces.com"; var region = "cn-beijing"; // 填写 BucketName var bucketName = "*** Provide your bucket name ***"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { // 创建存储空间输入 var createBucketInput = new CreateBucketInput() { Bucket = bucketName, // 指定存储桶为多 AZ AzRedundancy = AzRedundancyType.AzRedundancyMultiAz }; // 创建存储空间。 var createBucketOutput = client.CreateBucket(createBucketInput); Console.WriteLine("Create bucket succeeded, request id {0} ", createBucketOutput.RequestID); Console.WriteLine("Create bucket succeeded, status code {0} ", createBucketOutput.StatusCode); } catch (TosServerException ex) { Console.WriteLine("Create bucket failed, request id {0}", ex.RequestID); Console.WriteLine("Create bucket failed, status code {0}", ex.StatusCode); Console.WriteLine("Create bucket failed, response error code {0}", ex.Code); Console.WriteLine("Create bucket failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("Create bucket failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("Create bucket failed, {0}", ex.Message); } } } }
关于创建桶的 API 接口介绍,请参见 CreateBucket。