TOS 中只有对象的概念,内部使用扁平结构存储数据,为方便您对对象进行分组并简化权限管理,您可以使用目录层次来组织对象。
以下代码用于创建目录。
using System; using System.IO; using System.Text; 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 { // 创建目录 using (MemoryStream requestContent = new MemoryStream(Encoding.UTF8.GetBytes(""))) { var putObjectInput = new PutObjectInput() { Bucket = bucketName, Key = "exampledir/", Content = requestContent }; var putObjectOutput = client.PutObject(putObjectInput); Console.WriteLine("Put object succeeded, ETag: {0} ", putObjectOutput.ETag); } // 创建多级目录 using (MemoryStream requestContent = new MemoryStream(Encoding.UTF8.GetBytes(""))) { var putObjectInput = new PutObjectInput() { Bucket = bucketName, Key = "exampledir/test/", Content = requestContent }; var putObjectOutput = client.PutObject(putObjectInput); Console.WriteLine("Put object succeeded, ETag: {0} ", putObjectOutput.ETag); } } catch (TosServerException ex) { Console.WriteLine("Put object failed, request id {0}", ex.RequestID); Console.WriteLine("Put object failed, status code {0}", ex.StatusCode); Console.WriteLine("Put object failed, response error code {0}", ex.Code); Console.WriteLine("Put object failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("Put object failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("Put object failed, {0}", ex.Message); } } } }
TOS 只有对象的概念, 可通过创建一个大小为 0 并且以斜线 / 结尾的对象,模拟目录的功能。
通过 Delimiter
和 Prefix
两个参数可以模拟目录的功能,说明如下:
Delimiter
为 /
,同时设置 Prefix
为空, 将返回根目录下的对象和子目录信息。Delimiter
为 /
,同时设置 Prefix
为子目录 subfiledir
,将返回子目录的对象和次级目录。以下代码用于列举根目录下的对象和子目录。
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 truncated = true; var marker = ""; while (truncated) { var listObjectsInput = new ListObjectsInput() { Bucket = bucketName, MaxKeys = 1000, Delimiter = "/", Marker = marker }; var listObjectsOutput = client.ListObjects(listObjectsInput); // 返回根目录下的子目录 foreach (var obj in listObjectsOutput.CommonPrefixes) { Console.WriteLine("Sub Dir: {0}", obj.Prefix); } // Contents 中返回根目录下的对象 foreach (var obj in listObjectsOutput.Contents) { Console.WriteLine("Obj: {0}", obj.Key); } truncated = listObjectsOutput.IsTruncated; marker = listObjectsOutput.NextMarker; } } catch (TosServerException ex) { Console.WriteLine("ListObjects failed, request id {0}", ex.RequestID); Console.WriteLine("ListObjects failed, status code {0}", ex.StatusCode); Console.WriteLine("ListObjects failed, response error code {0}", ex.Code); Console.WriteLine("ListObjects failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("ListObjects failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("ListObjects failed, {0}", ex.Message); } } } }
以下代码用于删除存储桶中 exampledir/
目录下的所有文件。
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 truncated = true; var marker = ""; while (truncated) { var listObjectsInput = new ListObjectsInput() { Bucket = bucketName, MaxKeys = 1000, Prefix = "exampledir/", Marker = marker }; var listObjectsOutput = client.ListObjects(listObjectsInput); // Contents 中返回根目录下的对象 foreach (var obj in listObjectsOutput.Contents) { var deleteObjectInput = new DeleteObjectInput() { Bucket = bucketName, Key = obj.Key }; var deleteObjectOutput = client.DeleteObject(deleteObjectInput); Console.WriteLine("DeleteObject Request ID: {0}", deleteObjectOutput.RequestID); } truncated = listObjectsOutput.IsTruncated; marker = listObjectsOutput.NextMarker; } } catch (TosServerException ex) { Console.WriteLine("DeleteObject failed, request id {0}", ex.RequestID); Console.WriteLine("DeleteObject failed, status code {0}", ex.StatusCode); Console.WriteLine("DeleteObject failed, response error code {0}", ex.Code); Console.WriteLine("DeleteObject failed, response error message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("DeleteObject failed, error message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("DeleteObject failed, {0}", ex.Message); } } } }