当对象不会再使用时,您可以删除对象,降低成本开销。目前 TOS .NET SDK 支持删除桶中的单个对象、删除指定的多个对象,对于开启多版本的桶,你可指定删除对象的任意版本数据。
以下代码用于删除指定对象。
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 ***"; // 填写对象名 var objectKey = "*** Provide your object key ***"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { // 删除存储桶中指定对象 var deleteObjectInput = new DeleteObjectInput() { Bucket = bucketName, Key = objectKey }; var deleteObjectOutput = client.DeleteObject(deleteObjectInput); Console.WriteLine("DeleteObject succeeded, Request ID {0}", deleteObjectOutput.RequestID); } 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); } } } }
以下代码用于删除指定版本的对象。
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 ***"; // 填写对象名 var objectKey = "*** Provide your object key ***"; // 开启多版本后,在上传对象后会返回对象 Version ID var versionID = "*** Provide object Version ID"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { // 删除存储桶中指定对象指定版本 var deleteObjectInput = new DeleteObjectInput() { Bucket = bucketName, Key = objectKey, VersionID = versionID }; var deleteObjectOutput = client.DeleteObject(deleteObjectInput); Console.WriteLine("DeleteObject succeeded, Request ID {0}", deleteObjectOutput.RequestID); } 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); } } } }
注意
versionId
,将插入删除标记。如果指定了 versionId
,将永久删除该对象的指定版本。quiet
进行设置。如果 quiet
为 false
,将在返回响应时,不管对象是否删除成功都将删除结果包含在响应里;如果 quiet
为 true
时,将在返回响应时,只返回删除失败的对象结果,没有返回的认为删除成功。以下代码用于批量删除对象。
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 ***"; // 指定的 objectKey var objectKey1 = "*** Provide Object Key ***"; var objectKey2 = "*** Provide Object Key ***"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { var objects = new[] { new ObjectTobeDeleted() { Key = objectKey1 }, new ObjectTobeDeleted() { Key = objectKey2 }, }; // 批量删除存储桶中指定对象 var deleteMultiObjectsInput = new DeleteMultiObjectsInput() { Bucket = bucketName, Objects = objects, }; var deleteMultiObjectsOutput = client.DeleteMultiObjects(deleteMultiObjectsInput); Console.WriteLine("DeleteMultiObjects succeeded, Request ID {0}", deleteMultiObjectsOutput.RequestID); } catch (TosServerException ex) { Console.WriteLine("DeleteMultiObjects failed, Request ID {0}", ex.RequestID); Console.WriteLine("DeleteMultiObjects failed, Status Code {0}", ex.StatusCode); Console.WriteLine("DeleteMultiObjects failed, Response Error Code {0}", ex.Code); Console.WriteLine("DeleteMultiObjects failed, Response Error Message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("DeleteMultiObjects failed, Error Message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("DeleteMultiObjects 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 ***"; // 指定的 objectKey var objectKey1 = "*** Provide Object Key ***"; var objectKey2 = "*** Provide Object Key ***"; // 指定的 objectKey 所对应的 object 版本号 var versionID1 = "*** Provide objectKey1 Version ID ***"; var versionID2 = "*** Provide objectKey2 Version ID ***"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { var objects = new[] { new ObjectTobeDeleted() { Key = objectKey1, VersionID = versionID1 }, new ObjectTobeDeleted() { Key = objectKey2, VersionID = versionID2 }, }; // 批量删除存储桶中指定对象指定版本 var deleteMultiObjectsInput = new DeleteMultiObjectsInput() { Bucket = bucketName, Objects = objects, }; var deleteMultiObjectsOutput = client.DeleteMultiObjects(deleteMultiObjectsInput); Console.WriteLine("DeleteMultiObjects succeeded, Request ID {0}", deleteMultiObjectsOutput.RequestID); } catch (TosServerException ex) { Console.WriteLine("DeleteMultiObjects failed, Request ID {0}", ex.RequestID); Console.WriteLine("DeleteMultiObjects failed, Status Code {0}", ex.StatusCode); Console.WriteLine("DeleteMultiObjects failed, Response Error Code {0}", ex.Code); Console.WriteLine("DeleteMultiObjects failed, Response Error Message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("DeleteMultiObjects failed, Error Message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("DeleteMultiObjects 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 ***"; // 填写对象名 var objectKey = "*** Provide your object key ***"; // 创建TOSClient实例 var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build(); try { // 删除桶中所有对象 var keyMarker = ""; var isTruncated = true; while (isTruncated) { var listObjectVersionsInput = new ListObjectVersionsInput() { Bucket = bucketName, KeyMarker = keyMarker, }; var listObjectVersionsOutput = client.ListObjectVersions(listObjectVersionsInput); // 删除多版本对象 foreach (var obj in listObjectVersionsOutput.Versions) { var deleteObjectInput = new DeleteObjectInput() { Bucket = bucketName, Key = obj.Key, VersionID = obj.VersionID }; var deleteObjectOutput = client.DeleteObject(deleteObjectInput); Console.WriteLine("Delete {0}, Request ID {1}", obj.Key, deleteObjectOutput.RequestID); } // 删除标记信息 foreach (var deleteMarker in listObjectVersionsOutput.DeleteMarkers) { var deleteObjectInput = new DeleteObjectInput() { Bucket = bucketName, Key = deleteMarker.Key, VersionID = deleteMarker.VersionID }; var deleteObjectOutput = client.DeleteObject(deleteObjectInput); Console.WriteLine("Delete {0}, Request ID {1}", deleteMarker.Key, deleteObjectOutput.RequestID); } isTruncated = listObjectVersionsOutput.IsTruncated; keyMarker = listObjectVersionsOutput.NextKeyMarker; } } catch (TosServerException ex) { Console.WriteLine("Clear Bucket failed, Request ID {0}", ex.RequestID); Console.WriteLine("Clear Bucket failed, Status Code {0}", ex.StatusCode); Console.WriteLine("Clear Bucket failed, Response Error Code {0}", ex.Code); Console.WriteLine("Clear Bucket failed, Response Error Message {0}", ex.Message); } catch (TosClientException ex) { Console.WriteLine("Clear Bucket failed, Error Message {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("Clear Bucket failed, {0}", ex.Message); } } } }
关于删除对象的 API 接口详细介绍,请参见 DeleteObject。