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

本文介绍如何通过 ListObject 列举指定桶下的所有对象、指定前缀文件、指定目录下的文件和子目录。

参数说明

参数

描述

Prefix

本次查询结果的前缀。

Delimiter

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

Marker

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

MaxKeys

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

EncodingType

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

示例代码

简单列举

以下代码用于列举指定桶中的 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
            {
                var listObjectsInput = new ListObjectsInput()
                {
                    Bucket = bucketName,
                    MaxKeys = 10
                };

                // 获取 bucket 中最多 10 个对象
                var listObjectsOutput = client.ListObjects(listObjectsInput);
                foreach (var obj in listObjectsOutput.Contents)
                {
                    Console.WriteLine("Object Key: {0}", obj.Key);
                    // 对象最后修改时间
                    Console.WriteLine("Object LastModified: {0}", obj.LastModified);
                    // 对象 Etag
                    Console.WriteLine("Object ETag: {0}", obj.ETag);
                    // 对象大小
                    Console.WriteLine("Object Size: {0}", obj.Size);
                    // 对象 Owner
                    Console.WriteLine("Object Owner: {0}", obj.Owner);
                    // 对象存储类型
                    Console.WriteLine("Object StorageClass: {0}", obj.StorageClass);
                    // 对象 CRC64
                    Console.WriteLine("Object HashCrc64ecma: {0}", obj.HashCrc64ecma);
                }
            }
            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);
            }
        }
    }
}

分页列举

以下代码用于分页列举指定存储桶中的对象,包括获取桶中前 1000 个对象,分页获取后 1000 个对象。

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 listObjectsInput = new ListObjectsInput()
                {
                    Bucket = bucketName,
                    MaxKeys = 1000
                };

                // 获取 bucket 中前 1000 个对象
                var listObjectsOutput = client.ListObjects(listObjectsInput);
                foreach (var obj in listObjectsOutput.Contents)
                {
                    Console.WriteLine("Object Key: {0}", obj.Key);
                    // 对象最后修改时间
                    Console.WriteLine("Object LastModified: {0}", obj.LastModified);
                    // 对象 Etag
                    Console.WriteLine("Object ETag: {0}", obj.ETag);
                    // 对象大小
                    Console.WriteLine("Object Size: {0}", obj.Size);
                    // 对象 Owner
                    Console.WriteLine("Object Owner: {0}", obj.Owner);
                    // 对象存储类型
                    Console.WriteLine("Object StorageClass: {0}", obj.StorageClass);
                    // 对象 CRC64
                    Console.WriteLine("Object HashCrc64ecma: {0}", obj.HashCrc64ecma);
                }

                // 分页获取后 1000 个对象
                listObjectsInput = new ListObjectsInput()
                {
                    Bucket = bucketName,
                    MaxKeys = 1000,
                    Marker = listObjectsOutput.NextMarker
                };
                listObjectsOutput = client.ListObjects(listObjectsInput);

                foreach (var obj in listObjectsOutput.Contents)
                {
                    Console.WriteLine("Object Key: {0}", obj.Key);
                    // 对象最后修改时间
                    Console.WriteLine("Object LastModified: {0}", obj.LastModified);
                    // 对象 Etag
                    Console.WriteLine("Object ETag: {0}", obj.ETag);
                    // 对象大小
                    Console.WriteLine("Object Size: {0}", obj.Size);
                    // 对象 Owner
                    Console.WriteLine("Object Owner: {0}", obj.Owner);
                    // 对象存储类型
                    Console.WriteLine("Object StorageClass: {0}", obj.StorageClass);
                    // 对象 CRC64
                    Console.WriteLine("Object HashCrc64ecma: {0}", obj.HashCrc64ecma);
                }
            }
            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);
            }
        }
    }
}

列举指定前缀的所有对象

以下代码用于列举存储桶指定前缀的所有对象。

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 = "";

                while (truncated)
                {
                    var listObjectsInput = new ListObjectsInput()
                    {
                        Bucket = bucketName,
                        Prefix = prefix,
                        MaxKeys = 1000,
                        Marker = marker
                    };

                    var listObjectsOutput = client.ListObjects(listObjectsInput);
                    foreach (var obj in listObjectsOutput.Contents)
                    {
                        Console.WriteLine("Object Key: {0}", obj.Key);
                        // 对象最后修改时间
                        Console.WriteLine("Object LastModified: {0}", obj.LastModified);
                        // 对象 Etag
                        Console.WriteLine("Object ETag: {0}", obj.ETag);
                        // 对象大小
                        Console.WriteLine("Object Size: {0}", obj.Size);
                        // 对象 Owner
                        Console.WriteLine("Object Owner: {0}", obj.Owner);
                        // 对象存储类型
                        Console.WriteLine("Object StorageClass: {0}", obj.StorageClass);
                        // 对象 CRC64
                        Console.WriteLine("Object HashCrc64ecma: {0}", obj.HashCrc64ecma);
                    }

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

分页列举桶内所有对象

以下代码用于分页列举指定存储桶中所有对象。

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 下的所有对象
                var truncated = true;
                var marker = "";

                while (truncated)
                {
                    var listObjectsInput = new ListObjectsInput()
                    {
                        Bucket = bucketName,
                        MaxKeys = 1000,
                        Marker = marker
                    };

                    var listObjectsOutput = client.ListObjects(listObjectsInput);
                    foreach (var obj in listObjectsOutput.Contents)
                    {
                        Console.WriteLine("Object Key: {0}", obj.Key);
                        // 对象最后修改时间
                        Console.WriteLine("Object LastModified: {0}", obj.LastModified);
                        // 对象 Etag
                        Console.WriteLine("Object ETag: {0}", obj.ETag);
                        // 对象大小
                        Console.WriteLine("Object Size: {0}", obj.Size);
                        // 对象 Owner
                        Console.WriteLine("Object Owner: {0}", obj.Owner);
                        // 对象存储类型
                        Console.WriteLine("Object StorageClass: {0}", obj.StorageClass);
                        // 对象 CRC64
                        Console.WriteLine("Object HashCrc64ecma: {0}", obj.HashCrc64ecma);
                    }

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

列举指定目录下的文件和子目录

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 = "";

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

                    // 返回根目录下的对象
                    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);
            }
        }
    }
}