Exemplo n.º 1
0
 public bool CutFile(String bucketName, String sourceKey, String targetPath)
 {
     try
     {
         BosClient client = this.GenerateBosClient();
         //当是文件的时候
         if (sourceKey.EndsWith(this.delimter))
         {
             String filter = sourceKey.Substring(0, sourceKey.Length - 1);
             if (filter.IndexOf(this.delimter) >= 0)
             {
                 filter = sourceKey.Substring(0, filter.IndexOf(this.delimter)) + this.delimter;
             }
             else
             {
                 filter = String.Empty;
             }
             ListObjectsResponse response = client.ListObjects(bucketName, sourceKey);
             foreach (BosObjectSummary obj in response.Contents)
             {
                 String key    = obj.Key;
                 String newKey = "";
                 if (filter.Length == 0)
                 {
                     newKey = targetPath + key;
                 }
                 else
                 {
                     newKey = targetPath + key.Replace(filter, "");
                 }
                 client.CopyObject(bucketName, key, bucketName, newKey);
                 client.DeleteObject(bucketName, key);
             }
         }
         else
         {
             String fileName = sourceKey;
             if (sourceKey.IndexOf(this.delimter) >= 0)
             {
                 fileName = sourceKey.Substring(sourceKey.IndexOf(this.delimter));
             }
             String newKey = targetPath + fileName;
             client.CopyObject(bucketName, sourceKey, bucketName, newKey);
             client.DeleteObject(bucketName, sourceKey);
         }
         // client.CopyObject();
         return(true);
     }
     catch (Exception)
     {
         throw;
     }
 }
Exemplo n.º 2
0
        private static void Main(string[] args)
        {
            BosClient    client     = GenerateBosClient();
            const string bucketName = "sample-bucket-copyobject3"; //示例Bucket名称

            // 1. 初始化:创建示例Bucket和Object
            client.CreateBucket(bucketName); //指定Bucket名称
            string objectName = "sample";

            client.PutObject(bucketName, objectName, "sampledata");

            // 2. 普通拷贝并打印结果
            string             newObjectName      = "copyobject";
            CopyObjectResponse copyObjectResponse = client.CopyObject(bucketName, objectName, bucketName,
                                                                      newObjectName);

            // sampledata
            Console.WriteLine(Encoding.Default.GetString(client.GetObjectContent(bucketName, newObjectName)));

            // 3. 拷贝并设置新的meta
            newObjectName = "copyobject-newmeta";
            CopyObjectRequest copyObjectRequest = new CopyObjectRequest()
            {
                SourceBucketName = bucketName,
                SourceKey        = objectName,
                BucketName       = bucketName,
                Key = newObjectName
            };
            Dictionary <String, String> userMetadata = new Dictionary <String, String>();

            userMetadata["usermetakey"] = "usermetavalue";
            ObjectMetadata objectMetadata = new ObjectMetadata()
            {
                UserMetadata = userMetadata
            };

            copyObjectRequest.NewObjectMetadata = objectMetadata;
            client.CopyObject(copyObjectRequest);
            // usermetavalue
            Console.WriteLine(client.GetObjectMetadata(bucketName, newObjectName).UserMetadata["usermetakey"]);
        }