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

该接口用于列举桶中的多版本对象。每次请求返回桶的部分多版本对象(单次请求最多 1000 个),可以使用请求参数作为选择标准来返回桶中对象的子集。

注意事项

列举多版本对象之前,您需要确保账号拥有桶的 tos:ListBucketVersions 权限,具体操作请参见 IAM策略概述

参数说明

参数

描述

Prefix

本次查询结果的前缀。

Delimiter

对对象名称进行分组的字符。

MaxKeys

返回列举对象的最大数。
取值:大于 0 小于等于 1000
默认值:1000

EncodingType

对返回的内容进行编码并指定编码的类型。
默认值:None

KeyMarker

列举多版本对象的起始位置。设定从该值之后按字母排序返回对象列表。通常为上次请求返回的 next_marker 值。

VersionIdMarker

KeyMarker 配合使用,设定从该值之后按字典排序返回多版本对象列表。从上次列举结果中 VersionIdMarker 获取。

示例代码

简单列举

以下代码用于列举指定存储桶中最多 10 个多版本对象。

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
            {
                // 获取 bucket 中最多 10 个多版本对象
                var listObjectVersionsInput = new ListObjectVersionsInput()
                {
                    Bucket = bucketName,
                    MaxKeys = 10
                };
                var listObjectVersionsOutput = client.ListObjectVersions(listObjectVersionsInput);
                foreach (var obj in listObjectVersionsOutput.Versions)
                {
                    Console.WriteLine("Object Key: {0}", obj.Key);
                    Console.WriteLine("Object VersionID: {0}", obj.VersionID);
                    Console.WriteLine("Object Last Modified: {0}", obj.LastModified);
                    Console.WriteLine("Object Is Latest Version: {0}", obj.IsLatest);
                }

                foreach (var deleteMarker in listObjectVersionsOutput.DeleteMarkers)
                {
                    Console.WriteLine("Delete Marker Key: {0}", deleteMarker.Key);
                    Console.WriteLine("Delete Marker VersionID: {0}", deleteMarker.VersionID);
                    Console.WriteLine("Delete Marker Last Modified: {0}", deleteMarker.LastModified);
                    Console.WriteLine("Delete Marker Is Latest Version: {0}", deleteMarker.IsLatest);
                }
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("ListObjectVersions failed, request id {0}", ex.RequestID);
                Console.WriteLine("ListObjectVersions failed, status code {0}", ex.StatusCode);
                Console.WriteLine("ListObjectVersions failed, response error code {0}", ex.Code);
                Console.WriteLine("ListObjectVersions failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("ListObjectVersions failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ListObjectVersions 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 prefix = "*** Provide your object key prefix ***";

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

            try
            {
                // 列举指定前缀的对象
                var truncated = true;
                var marker = "";
                var versionIdMarker = "";

                while (truncated)
                {
                    var listObjectVersionsInput = new ListObjectVersionsInput()
                    {
                        Bucket = bucketName,
                        MaxKeys = 10,
                        Prefix = prefix,
                        KeyMarker = marker,
                        VersionIDMarker = versionIdMarker
                    };
                    var listObjectVersionsOutput = client.ListObjectVersions(listObjectVersionsInput);
                    foreach (var obj in listObjectVersionsOutput.Versions)
                    {
                        Console.WriteLine("Object Key: {0}", obj.Key);
                        Console.WriteLine("Object VersionID: {0}", obj.VersionID);
                        Console.WriteLine("Object Last Modified: {0}", obj.LastModified);
                        Console.WriteLine("Object Is Latest Version: {0}", obj.IsLatest);
                    }

                    foreach (var deleteMarker in listObjectVersionsOutput.DeleteMarkers)
                    {
                        Console.WriteLine("Delete Marker Key: {0}", deleteMarker.Key);
                        Console.WriteLine("Delete Marker VersionID: {0}", deleteMarker.VersionID);
                        Console.WriteLine("Delete Marker Last Modified: {0}", deleteMarker.LastModified);
                        Console.WriteLine("Delete Marker Is Latest Version: {0}", deleteMarker.IsLatest);
                    }

                    truncated = listObjectVersionsOutput.IsTruncated;
                    marker = listObjectVersionsOutput.NextKeyMarker;
                    versionIdMarker = listObjectVersionsOutput.NextVersionIDMarker;
                }
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("ListObjectVersions failed, request id {0}", ex.RequestID);
                Console.WriteLine("ListObjectVersions failed, status code {0}", ex.StatusCode);
                Console.WriteLine("ListObjectVersions failed, response error code {0}", ex.Code);
                Console.WriteLine("ListObjectVersions failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("ListObjectVersions failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ListObjectVersions 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 truncated = true;
                var marker = "";
                var versionIdMarker = "";

                while (truncated)
                {
                    var listObjectVersionsInput = new ListObjectVersionsInput()
                    {
                        Bucket = bucketName,
                        MaxKeys = 1000,
                        KeyMarker = marker,
                        VersionIDMarker = versionIdMarker
                    };
                    var listObjectVersionsOutput = client.ListObjectVersions(listObjectVersionsInput);
                    foreach (var obj in listObjectVersionsOutput.Versions)
                    {
                        Console.WriteLine("Object Key: {0}", obj.Key);
                        Console.WriteLine("Object VersionID: {0}", obj.VersionID);
                        Console.WriteLine("Object Last Modified: {0}", obj.LastModified);
                        Console.WriteLine("Object Is Latest Version: {0}", obj.IsLatest);
                    }

                    foreach (var deleteMarker in listObjectVersionsOutput.DeleteMarkers)
                    {
                        Console.WriteLine("Delete Marker Key: {0}", deleteMarker.Key);
                        Console.WriteLine("Delete Marker VersionID: {0}", deleteMarker.VersionID);
                        Console.WriteLine("Delete Marker Last Modified: {0}", deleteMarker.LastModified);
                        Console.WriteLine("Delete Marker Is Latest Version: {0}", deleteMarker.IsLatest);
                    }

                    truncated = listObjectVersionsOutput.IsTruncated;
                    marker = listObjectVersionsOutput.NextKeyMarker;
                    versionIdMarker = listObjectVersionsOutput.NextVersionIDMarker;
                }
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("ListObjectVersions failed, request id {0}", ex.RequestID);
                Console.WriteLine("ListObjectVersions failed, status code {0}", ex.StatusCode);
                Console.WriteLine("ListObjectVersions failed, response error code {0}", ex.Code);
                Console.WriteLine("ListObjectVersions failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("ListObjectVersions failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ListObjectVersions failed, {0}", ex.Message);
            }
        }
    }
}

列举目录下的对象和子目录

TOS 只有对象的概念, 可通过创建一个大小为 0 并且以斜线 / 结尾的对象,模拟目录的功能。
通过 DelimiterPrefix 两个参数可以模拟目录的功能,说明如下:

  • 设置 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 = "";
                var versionIdMarker = "";
                var delimiter = "/";

                while (truncated)
                {
                    var listObjectVersionsInput = new ListObjectVersionsInput()
                    {
                        Bucket = bucketName,
                        MaxKeys = 1000,
                        KeyMarker = marker,
                        VersionIDMarker = versionIdMarker,
                        Delimiter = delimiter
                    };
                    var listObjectVersionsOutput = client.ListObjectVersions(listObjectVersionsInput);

                    // 返回根目录下的子目录
                    foreach (var prefix in listObjectVersionsOutput.CommonPrefixes)
                    {
                        Console.WriteLine("Sub Dir: {0}", prefix.Prefix);
                    }

                    // 列举根目录下的多版本对象
                    foreach (var obj in listObjectVersionsOutput.Versions)
                    {
                        Console.WriteLine("Object Key: {0}", obj.Key);
                        Console.WriteLine("Object VersionID: {0}", obj.VersionID);
                        Console.WriteLine("Object Last Modified: {0}", obj.LastModified);
                        Console.WriteLine("Object Is Latest Version: {0}", obj.IsLatest);
                    }

                    // 列举根目录下的删除标记
                    foreach (var deleteMarker in listObjectVersionsOutput.DeleteMarkers)
                    {
                        Console.WriteLine("Delete Marker Key: {0}", deleteMarker.Key);
                        Console.WriteLine("Delete Marker VersionID: {0}", deleteMarker.VersionID);
                        Console.WriteLine("Delete Marker Last Modified: {0}", deleteMarker.LastModified);
                        Console.WriteLine("Delete Marker Is Latest Version: {0}", deleteMarker.IsLatest);
                    }

                    truncated = listObjectVersionsOutput.IsTruncated;
                    marker = listObjectVersionsOutput.NextKeyMarker;
                    versionIdMarker = listObjectVersionsOutput.NextVersionIDMarker;
                }
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("ListObjectVersions failed, request id {0}", ex.RequestID);
                Console.WriteLine("ListObjectVersions failed, status code {0}", ex.StatusCode);
                Console.WriteLine("ListObjectVersions failed, response error code {0}", ex.Code);
                Console.WriteLine("ListObjectVersions failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("ListObjectVersions failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ListObjectVersions failed, {0}", ex.Message);
            }
        }
    }
}

相关文档

关于列举多版本对象的 API 接口详细介绍,请参见 ListObjectsVersions