You need to enable JavaScript to run this app.
导航
创建桶(.NET SDK)
最近更新时间:2024.02.04 18:31:07首次发布时间:2023.08.24 14:41:46

存储桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器储存对象(Object)数据。新创建的桶名在 TOS 中必须是唯一的。本文介绍如何创建桶。

注意事项

  • 存储桶的命名规范如下:
    • 只能包括小写字母 a~z、数字和短横线(-)。
    • 开头和结尾只能是数字或字母。
    • 长度必须在 3~63 个字符之间。
  • 通过与 TOS 同地域的其他火山引擎产品访问 TOS 时,推荐您使用内网 Endpoint。关于地域和 Endpoint 的更多信息,请参见地域和访问域名
  • 创建存储桶前,您需要确保账号拥有 tos:CreateBucket 权限。您可以通过 IAM 策略授予相应权限,具体步骤,请参见 IAM 策略

示例代码

指定 ACL

以下代码用于在创建存储桶时指定 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 类型

以下代码展示用于在创建存储桶时指定 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