/// 设置存储桶标签
        public void PutBucketTagging()
        {
            //.cssg-snippet-body-start:[put-bucket-tagging]
            try
            {
                string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
                PutBucketTaggingRequest request = new PutBucketTaggingRequest(bucket);
                string akey   = "aTagKey";
                string avalue = "aTagValue";
                string bkey   = "bTagKey";
                string bvalue = "bTagValue";

                request.AddTag(akey, avalue);
                request.AddTag(bkey, bvalue);

                //执行请求
                PutBucketTaggingResult result = cosXml.PutBucketTagging(request);

                //请求成功
                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }
            //.cssg-snippet-body-end
        }
Example #2
0
        public void TestBucketTagging()
        {
            try
            {
                // 设置 tag
                PutBucketTaggingRequest request = new PutBucketTaggingRequest(bucket);


                string akey   = "aTagKey";
                string avalue = "aTagValue";
                string bkey   = "bTagKey";
                string bvalue = "bTagValue";

                request.AddTag(akey, avalue);
                request.AddTag(bkey, bvalue);

                PutBucketTaggingResult result = cosXml.PutBucketTagging(request);

                Assert.True(result.IsSuccessful());

                // 获取 tag
                GetBucketTaggingRequest getRequest = new GetBucketTaggingRequest(bucket);

                GetBucketTaggingResult getResult = cosXml.GetBucketTagging(getRequest);

                // Console.WriteLine(getResult.GetResultInfo());
                Assert.IsNotEmpty((result.GetResultInfo()));

                Tagging tagging = getResult.tagging;

                Assert.AreEqual(getResult.httpCode, 200);
                Assert.AreEqual(tagging.tagSet.tags.Count, 2);

                foreach (Tagging.Tag tag in tagging.tagSet.tags)
                {
                    if (tag.key.Equals(akey))
                    {
                        Assert.AreEqual(avalue, tag.value);
                    }
                    else if (tag.key.Equals(bkey))
                    {
                        Assert.AreEqual(bvalue, tag.value);
                    }
                    else
                    {
                        Assert.Fail();
                    }
                }

                // 删除 tag
                DeleteBucketTaggingRequest deleteRequest = new DeleteBucketTaggingRequest(bucket);

                DeleteBucketTaggingResult deleteResult = cosXml.DeleteBucketTagging(deleteRequest);
                Assert.True(deleteResult.IsSuccessful());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                Console.WriteLine("CosClientException: " + clientEx.Message);
                Assert.Fail();
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                Assert.Fail();
            }

            try
            {
                // 验证删除成功
                GetBucketTaggingRequest getRequest = new GetBucketTaggingRequest(bucket);

                GetBucketTaggingResult getResult = cosXml.GetBucketTagging(getRequest);
                Assert.Fail();
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                Console.WriteLine("CosClientException: " + clientEx.Message);
                Assert.Fail();
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                Assert.AreEqual(serverEx.statusCode, 404);
            }
        }