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 .NET SDK 的 listBuckets 接口列举当前账号所有地域下符合指定条件的存储桶。

注意事项

  • 列举桶前,您需要确保账号已拥有 tos: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