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

版本控制应用于桶中所有对象。通过开启桶的版本控制,您可以在错误删除或者覆盖对象后,将对象回恢复至任意的历史版本。目前支持多版本的接口包含 GetObject、GetObjectACL、CopyObject、UploadPartCopy、DeleteObject等。调用相关接口时,您可通过可选参数 VersionID 指定操作对象的具体版本。

下载多版本对象

以下版本用于下载指定版本对象。

using System;
using System.IO;
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 = "example_dir/example.txt";
            // 指定下载对象版本信息
            var versionID = "your object version id";

            // 创建TOSClient实例
            var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();

            try
            {
                var getObjectInput = new GetObjectInput()
                {
                    Bucket = bucketName,
                    Key = objectKey,
                    VersionID = versionID
                };
                var getObjectOutput = client.GetObject(getObjectInput);
                Console.WriteLine("GetObject succeeded, request id {0}", getObjectOutput.RequestID);
                // 下载数据大小
                Console.WriteLine("GetObject succeeded, content length {0}", getObjectOutput.ContentLength);
                // 完成下载
                using (var reader = new StreamReader(getObjectOutput.Content))
                {
                    Console.WriteLine("GetObject succeeded, content {0}", reader.ReadToEnd());
                    getObjectOutput.Content.Close();
                }
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("GetObject failed, request id {0}", ex.RequestID);
                Console.WriteLine("GetObject failed, status code {0}", ex.StatusCode);
                Console.WriteLine("GetObject failed, response error code {0}", ex.Code);
                Console.WriteLine("GetObject failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("GetObject failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("GetObject 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);
            }
        }
    }
}

清空开启多版本的桶

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

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);
            }
        }
    }
}