您可以通过 copy_object 接口,在同一地域的存储桶之间拷贝对象,也可以在同一个存储桶内拷贝对象。拷贝时可以指定新的元数据信息或保持源对象的元数据信息。
以下代码用于在同一地域的存储桶之间拷贝对象。
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 dstBucketName = "*** Provide your dst bucket name ***"; // 复制源对象 key var srcObjectKey = "srcObjectKey"; var dstObjectKey = "objectKey"; // 创建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); copyObjectInput = new CopyObjectInput() { Bucket = bucketName, Key = dstObjectKey, SrcBucket = bucketName, SrcKey = srcObjectKey, // 复制时指定为私有权限 ACL = ACLType.ACLPrivate, // 复制时指定对象为低频存储 StorageClass = StorageClassType.StorageClassIa, }; // 复制对象使用指定的元数据信息 copyObjectOutput = client.CopyObject(copyObjectInput); Console.WriteLine("CopyObject succeeded, request id {0}", copyObjectOutput.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); } } } }
关于普通拷贝的 API 接口的详细介绍,请参见 CopyObject。