桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。本文介绍如何通过 TOS .NET SDK 的 listBuckets
接口列举当前账号所有地域下符合指定条件的存储桶。
以下代码用于列举当前账号所有区域的桶。
using System; using TOS; using TOS.Error; 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"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { // 列举账号下 Bucket var listBucketsOutput = client.ListBuckets(); Console.WriteLine("List bucket succeeded, request id {0} ", listBucketsOutput.RequestID); Console.WriteLine("List bucket succeeded, status code {0} ", listBucketsOutput.StatusCode); foreach (var bucket in listBucketsOutput.Buckets) { Console.WriteLine("Bucket Name: {0}", bucket.Name); Console.WriteLine("Bucket CreationDate: {0}", bucket.CreationDate); Console.WriteLine("Bucket Location: {0}", bucket.Location); Console.WriteLine("Bucket ExtranetEndpoint: {0}", bucket.ExtranetEndpoint); Console.WriteLine("Bucket IntranetEndpoint: {0}", bucket.IntranetEndpoint); } } catch (TosServerException ex) { Console.WriteLine("List bucket failed, request id {0}", ex.RequestID); Console.WriteLine("List bucket failed, status code {0}", ex.StatusCode); Console.WriteLine("List bucket failed, response error code {0}", ex.Code); Console.WriteLine("List bucket failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("List bucket failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("List bucket failed, {0}", ex.Message); } } } }
关于列举桶的 API 接口的详细介绍,请参见 ListBuckets。