You need to enable JavaScript to run this app.
导航
删除场景(.NET SDK)
最近更新时间:2024.02.04 18:31:08首次发布时间:2023.08.24 14:41:47

本文介绍删除对象常用场景的示例代码。

删除目录

以下代码用于删除指定目录 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);
            }
        }
    }
}

清空桶

以下代码用于清空整个桶,包括删除所有对象、删除所有未合并的对象。

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 marker = "";
                var isTruncated = true;

                while (isTruncated)
                {
                    var listObjectInput = new ListObjectsInput()
                    {
                        Bucket = bucketName,
                        MaxKeys = 1000,
                        Marker = marker
                    };
                    var listObjectOutput = client.ListObjects(listObjectInput);

                    // 删除对象
                    foreach (var obj in listObjectOutput.Contents)
                    {
                        var deleteObjectInput = new DeleteObjectInput()
                        {
                            Bucket = bucketName,
                            Key = obj.Key,
                        };
                        var deleteObjectOutput = client.DeleteObject(deleteObjectInput);
                        Console.WriteLine("Delete {0}, Request ID {1}", obj.Key, deleteObjectOutput.RequestID);
                    }

                    isTruncated = listObjectOutput.IsTruncated;
                    marker = listObjectOutput.NextMarker;
                }

                // 终止所有未完成的分片
                marker = "";
                isTruncated = true;
                while (isTruncated)
                {
                    var listMultipartUploadsInput = new ListMultipartUploadsInput()
                    {
                        Bucket = bucketName,
                        MaxUploads = 1000,
                        KeyMarker = marker
                    };
                    var listMultipartUploadsOutput = client.ListMultipartUploads(listMultipartUploadsInput);

                    foreach (var upload in listMultipartUploadsOutput.Uploads)
                    {
                        var abortMultipartUploadInput = new AbortMultipartUploadInput()
                        {
                            Bucket = bucketName,
                            Key = upload.Key,
                            UploadID = upload.UploadID,
                        };
                        var abortMultipartUploadOutput = client.AbortMultipartUpload(abortMultipartUploadInput);
                        Console.WriteLine("AbortMultipartUpload UploadID {0}, Request ID {1}", upload.UploadID,
                            abortMultipartUploadOutput.RequestID);

                        isTruncated = listMultipartUploadsOutput.IsTruncated;
                        marker = listMultipartUploadsOutput.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);
            }
        }
    }
}

清空开启多版本的桶

以下代码用于清空整个开启多版本的存储桶,包括删除多个版本(包含删除标记)的对象、所有未合并的对象。

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,
                        MaxKeys = 1000,
                        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;
                }

                // 终止所有未完成的分片
                keyMarker = "";
                isTruncated = true;
                while (isTruncated)
                {
                    var listMultipartUploadsInput = new ListMultipartUploadsInput()
                    {
                        Bucket = bucketName,
                        MaxUploads = 1000,
                        KeyMarker = keyMarker
                    };
                    var listMultipartUploadsOutput = client.ListMultipartUploads(listMultipartUploadsInput);

                    foreach (var upload in listMultipartUploadsOutput.Uploads)
                    {
                        var abortMultipartUploadInput = new AbortMultipartUploadInput()
                        {
                            Bucket = bucketName,
                            Key = upload.Key,
                            UploadID = upload.UploadID,
                        };
                        var abortMultipartUploadOutput = client.AbortMultipartUpload(abortMultipartUploadInput);
                        Console.WriteLine("AbortMultipartUpload UploadID {0}, Request ID {1}", upload.UploadID,
                            abortMultipartUploadOutput.RequestID);

                        isTruncated = listMultipartUploadsOutput.IsTruncated;
                        keyMarker = listMultipartUploadsOutput.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);
            }
        }
    }
}