You need to enable JavaScript to run this app.
导航

移动及重命名场景(.NET SDK)

最近更新时间2024.02.04 18:31:08

首次发布时间2023.08.24 14:41:47

TOS 不支持直接对对象重命名。如果您需要在 Bucket 中对文件进行重命名,您可以通过 CopyObject 接口将源对象拷贝至目标对象,然后通过 DeleteObject 接口删除源对象。

重命名单个对象

以下代码用于将存储桶中对象 src-object 重命名为 dest-object。

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 ***";
            // 复制源对象 key
            var srcObjectKey = "src-object";
            var dstObjectKey = "dest-object";

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

            try
            {
                var copyObjectInput = new CopyObjectInput()
                {
                    Bucket = bucketName,
                    Key = dstObjectKey,
                    SrcBucket = bucketName,
                    SrcKey = srcObjectKey,
                };
                // 同一个 Bucket 复制对象,复制对象使用原有的元数据数据
                var copyObjectOutput = client.CopyObject(copyObjectInput);
                Console.WriteLine("CopyObject succeeded, request id {0}", copyObjectOutput.RequestID);

                // 删除源对象
                var deleteObjectInput = new DeleteObjectInput()
                {
                    Bucket = bucketName,
                    Key = srcObjectKey
                };
                var deleteObjectOutput = client.DeleteObject(deleteObjectInput);
                Console.WriteLine("DeleteObject succeeded, request id {0}", deleteObjectOutput.RequestID);
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("CopyObject failed, request id {0}", ex.RequestID);
                Console.WriteLine("CopyObject failed, status code {0}", ex.StatusCode);
                Console.WriteLine("CopyObject failed, response error code {0}", ex.Code);
                Console.WriteLine("CopyObject failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("CopyObject failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("CopyObject failed, {0}", ex.Message);
            }
        }
    }
}

重命名目录

以下代码用于将存储桶中 exampledir1/ 目录重命名为 exampledir2/

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 oldPrefix = "exampledir1/";
            var newPrefix = "exampledir2/";

            // 创建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,
                        Marker = marker,
                        Prefix = oldPrefix
                    };
                    var listObjectsOutput = client.ListObjects(listObjectsInput);

                    foreach (var obj in listObjectsOutput.Contents)
                    {
                        // 将源对象复制到目标对象
                        var newKey = newPrefix + obj.Key.Substring(oldPrefix.Length);
                        var copyObjectInput = new CopyObjectInput()
                        {
                            Bucket = bucketName,
                            Key = newKey,
                            SrcBucket = bucketName,
                            SrcKey = obj.Key
                        };
                        var copyObjectOutput = client.CopyObject(copyObjectInput);
                        Console.WriteLine("CopyObject succeeded, request id {0}", copyObjectOutput.RequestID);

                        // 删除源对象
                        var deleteObjectInput = new DeleteObjectInput()
                        {
                            Bucket = bucketName,
                            Key = obj.Key
                        };
                        var deleteObjectOutput = client.DeleteObject(deleteObjectInput);
                        Console.WriteLine("DeleteObject succeeded, request id {0}", deleteObjectOutput.RequestID);
                    }

                    truncated = listObjectsOutput.IsTruncated;
                    marker = listObjectsOutput.NextMarker;
                }
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("CopyObject failed, request id {0}", ex.RequestID);
                Console.WriteLine("CopyObject failed, status code {0}", ex.StatusCode);
                Console.WriteLine("CopyObject failed, response header {0}", ex.Header);
                Console.WriteLine("CopyObject failed, response error code {0}", ex.Code);
                Console.WriteLine("CopyObject failed, response error message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("CopyObject failed, error message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("CopyObject failed, {0}", ex.Message);
            }
        }
    }
}