通过 HeadBucket 获取存储桶(Bucket)的信息, 同时可以根据 HTTP Code 判断桶是否存在。本文介绍如何获取桶元数据,和判断桶是否存在。
以下代码用于判断桶是否存在。
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 headBucketInput = new HeadBucketInput() { Bucket = bucketName, }; // 查看存储桶元数据信息 var headBucketOutput = client.HeadBucket(headBucketInput); // 桶存在,查看请求信息和桶信息 Console.WriteLine("Head bucket succeeded, request id {0} ", headBucketOutput.RequestID); Console.WriteLine("Head bucket succeeded, status code {0} ", headBucketOutput.StatusCode); } catch (TosServerException ex) { // 判断桶是否存在 if (ex.StatusCode == 404) { // 存储桶不存在 Console.WriteLine("Bucket not found"); } Console.WriteLine("Head bucket failed, request id {0}", ex.RequestID); Console.WriteLine("Head bucket failed, status code {0}", ex.StatusCode); Console.WriteLine("Head bucket failed, response error code {0}", ex.Code); Console.WriteLine("Head bucket failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("Head bucket failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("Head bucket failed, {0}", ex.Message); } } } }
以下代码用于获取桶元数据信息。
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 headBucketInput = new HeadBucketInput() { Bucket = bucketName, }; // 查看存储桶元数据信息 var headBucketOutput = client.HeadBucket(headBucketInput); // 桶存在,查看请求信息和桶信息 Console.WriteLine("Head bucket succeeded, request id {0} ", headBucketOutput.RequestID); Console.WriteLine("Head bucket succeeded, status code {0} ", headBucketOutput.StatusCode); // 查看存储桶地域 Console.WriteLine("Bucket Region {0}", headBucketOutput.Region); // 查看存储桶存储类型 Console.WriteLine("Bucket Storage Class {0}", headBucketOutput.StorageClass); // 查看桶 AZ 类型 Console.WriteLine("Bucket Az Redundancy {0}", headBucketOutput.AzRedundancy); } catch (TosServerException ex) { // 判断桶是否存在 if (ex.StatusCode == 404) { // 存储桶不存在 Console.WriteLine("Bucket not found"); } Console.WriteLine("Head bucket failed, request id {0}", ex.RequestID); Console.WriteLine("Head bucket failed, status code {0}", ex.StatusCode); Console.WriteLine("Head bucket failed, response error code {0}", ex.Code); Console.WriteLine("Head bucket failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("Head bucket failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("Head bucket failed, {0}", ex.Message); } } } }
关于获取桶的元数据 API 接口的详细介绍,请参见 HeadBucket。