@@ -2778,3 +2778,229 @@ func (s *OssBucketSuite) TestUploadObjectWithWebpFormat(c *C) {
27782778 bucket .DeleteObject (objectName )
27792779 client .DeleteBucket (bucketName )
27802780}
2781+
2782+ func (s * OssBucketSuite ) TestPutObjectTagging (c * C ) {
2783+ // put object with tagging
2784+ objectName := objectNamePrefix + randStr (8 )
2785+ tag1 := Tag {
2786+ Key : randStr (8 ),
2787+ Value : randStr (16 ),
2788+ }
2789+ tag2 := Tag {
2790+ Key : randStr (8 ),
2791+ Value : randStr (16 ),
2792+ }
2793+ tagging := Tagging {
2794+ Tags : []Tag {tag1 , tag2 },
2795+ }
2796+ err := s .bucket .PutObject (objectName , strings .NewReader (randStr (1024 )), SetTagging (tagging ))
2797+ c .Assert (err , IsNil )
2798+
2799+ headers , err := s .bucket .GetObjectDetailedMeta (objectName )
2800+ taggingCount , err := strconv .Atoi (headers ["X-Oss-Tagging-Count" ][0 ])
2801+ c .Assert (err , IsNil )
2802+ c .Assert (taggingCount , Equals , 2 )
2803+
2804+ // copy object with default option
2805+ destObjectName := objectNamePrefix + randStr (8 )
2806+ _ , err = s .bucket .CopyObject (objectName , destObjectName )
2807+ c .Assert (err , IsNil )
2808+ headers , err = s .bucket .GetObjectDetailedMeta (destObjectName )
2809+ taggingCount , err = strconv .Atoi (headers ["X-Oss-Tagging-Count" ][0 ])
2810+ c .Assert (err , IsNil )
2811+ c .Assert (taggingCount , Equals , 2 )
2812+
2813+ // delete object tagging
2814+ err = s .bucket .DeleteObjectTagging (objectName )
2815+ c .Assert (err , IsNil )
2816+
2817+ // get object tagging again
2818+ taggingResult , err := s .bucket .GetObjectTagging (objectName )
2819+ c .Assert (err , IsNil )
2820+ c .Assert (len (taggingResult .Tags ), Equals , 0 )
2821+
2822+ // put tagging
2823+ tag := Tag {
2824+ Key : randStr (8 ),
2825+ Value : randStr (16 ),
2826+ }
2827+ tagging .Tags = []Tag {tag }
2828+ err = s .bucket .PutObjectTagging (objectName , tagging )
2829+ c .Assert (err , IsNil )
2830+
2831+ taggingResult , err = s .bucket .GetObjectTagging (objectName )
2832+ c .Assert (len (taggingResult .Tags ), Equals , 1 )
2833+ c .Assert (taggingResult .Tags [0 ].Key , Equals , tag .Key )
2834+ c .Assert (taggingResult .Tags [0 ].Value , Equals , tag .Value )
2835+
2836+ //put tagging, the length of the key exceeds 128
2837+ tag = Tag {
2838+ Key : randStr (129 ),
2839+ Value : randStr (16 ),
2840+ }
2841+ tagging .Tags = []Tag {tag }
2842+ err = s .bucket .PutObjectTagging (objectName , tagging )
2843+ c .Assert (err , NotNil )
2844+
2845+ //put tagging, the length of the value exceeds 256
2846+ tag = Tag {
2847+ Key : randStr (8 ),
2848+ Value : randStr (257 ),
2849+ }
2850+ tagging .Tags = []Tag {tag }
2851+ err = s .bucket .PutObjectTagging (objectName , tagging )
2852+ c .Assert (err , NotNil )
2853+
2854+ //put tagging, the lens of tags exceed 10
2855+ tagging .Tags = []Tag {}
2856+ for i := 0 ; i < 11 ; i ++ {
2857+ tag = Tag {
2858+ Key : randStr (8 ),
2859+ Value : randStr (16 ),
2860+ }
2861+ tagging .Tags = append (tagging .Tags , tag )
2862+ }
2863+ err = s .bucket .PutObjectTagging (objectName , tagging )
2864+ c .Assert (err , NotNil )
2865+
2866+ //put tagging, invalid value of tag key
2867+ tag = Tag {
2868+ Key : randStr (8 ) + "&" ,
2869+ Value : randStr (16 ),
2870+ }
2871+ tagging .Tags = []Tag {tag }
2872+ err = s .bucket .PutObjectTagging (objectName , tagging )
2873+ c .Assert (err , NotNil )
2874+
2875+ //put tagging, invalid value of tag value
2876+ tag = Tag {
2877+ Key : randStr (8 ),
2878+ Value : randStr (16 ) + "&" ,
2879+ }
2880+ tagging .Tags = []Tag {tag }
2881+ err = s .bucket .PutObjectTagging (objectName , tagging )
2882+ c .Assert (err , NotNil )
2883+
2884+ //put tagging, repeated tag keys
2885+ tag1 = Tag {
2886+ Key : randStr (8 ),
2887+ Value : randStr (16 ),
2888+ }
2889+ tag2 = Tag {
2890+ Key : tag1 .Key ,
2891+ Value : randStr (16 ),
2892+ }
2893+ tagging .Tags = []Tag {tag1 , tag2 }
2894+ err = s .bucket .PutObjectTagging (objectName , tagging )
2895+ c .Assert (err , NotNil )
2896+
2897+ s .bucket .DeleteObject (destObjectName )
2898+ s .bucket .DeleteObject (objectName )
2899+ }
2900+
2901+ func (s * OssBucketSuite ) TestGetObjectTagging (c * C ) {
2902+ // get object which has 2 tags
2903+ objectName := objectNamePrefix + randStr (8 )
2904+ tag1 := Tag {
2905+ Key : randStr (8 ),
2906+ Value : randStr (16 ),
2907+ }
2908+ tag2 := Tag {
2909+ Key : randStr (8 ),
2910+ Value : randStr (16 ),
2911+ }
2912+
2913+ taggingInfo := Tagging {
2914+ Tags : []Tag {tag1 , tag2 },
2915+ }
2916+
2917+ err := s .bucket .PutObject (objectName , strings .NewReader (randStr (1024 )), SetTagging (taggingInfo ))
2918+ c .Assert (err , IsNil )
2919+
2920+ tagging , err := s .bucket .GetObjectTagging (objectName )
2921+ c .Assert (len (tagging .Tags ), Equals , 2 )
2922+ if tagging .Tags [0 ].Key == tag1 .Key {
2923+ c .Assert (tagging .Tags [0 ].Value , Equals , tag1 .Value )
2924+ c .Assert (tagging .Tags [1 ].Key , Equals , tag2 .Key )
2925+ c .Assert (tagging .Tags [1 ].Value , Equals , tag2 .Value )
2926+ } else {
2927+ c .Assert (tagging .Tags [0 ].Key , Equals , tag2 .Key )
2928+ c .Assert (tagging .Tags [0 ].Value , Equals , tag2 .Value )
2929+ c .Assert (tagging .Tags [1 ].Key , Equals , tag1 .Key )
2930+ c .Assert (tagging .Tags [1 ].Value , Equals , tag1 .Value )
2931+ }
2932+
2933+ // get tagging of an object that is not exist
2934+ err = s .bucket .DeleteObject (objectName )
2935+ c .Assert (err , IsNil )
2936+ tagging , err = s .bucket .GetObjectTagging (objectName )
2937+ c .Assert (err , NotNil )
2938+ c .Assert (len (tagging .Tags ), Equals , 0 )
2939+
2940+ // get object which has no tag
2941+ objectName = objectNamePrefix + randStr (8 )
2942+ err = s .bucket .PutObject (objectName , strings .NewReader (randStr (1024 )))
2943+ c .Assert (err , IsNil )
2944+ tagging , err = s .bucket .GetObjectTagging (objectName )
2945+ c .Assert (err , IsNil )
2946+ c .Assert (len (tagging .Tags ), Equals , 0 )
2947+
2948+ // copy object, with tagging option
2949+ destObjectName := objectName + "-dest"
2950+ tagging .Tags = []Tag {tag1 , tag2 }
2951+ _ , err = s .bucket .CopyObject (objectName , destObjectName , SetTagging (taggingInfo ))
2952+ c .Assert (err , IsNil )
2953+ tagging , err = s .bucket .GetObjectTagging (objectName )
2954+ c .Assert (err , IsNil )
2955+ c .Assert (len (tagging .Tags ), Equals , 0 )
2956+
2957+ // copy object, with tagging option, the value of tagging directive is "REPLACE"
2958+ tagging .Tags = []Tag {tag1 , tag2 }
2959+ _ , err = s .bucket .CopyObject (objectName , destObjectName , SetTagging (taggingInfo ), TaggingDirective (TaggingReplace ))
2960+ c .Assert (err , IsNil )
2961+ tagging , err = s .bucket .GetObjectTagging (destObjectName )
2962+ c .Assert (err , IsNil )
2963+ c .Assert (len (tagging .Tags ), Equals , 2 )
2964+ if tagging .Tags [0 ].Key == tag1 .Key {
2965+ c .Assert (tagging .Tags [0 ].Value , Equals , tag1 .Value )
2966+ c .Assert (tagging .Tags [1 ].Key , Equals , tag2 .Key )
2967+ c .Assert (tagging .Tags [1 ].Value , Equals , tag2 .Value )
2968+ } else {
2969+ c .Assert (tagging .Tags [0 ].Key , Equals , tag2 .Key )
2970+ c .Assert (tagging .Tags [0 ].Value , Equals , tag2 .Value )
2971+ c .Assert (tagging .Tags [1 ].Key , Equals , tag1 .Key )
2972+ c .Assert (tagging .Tags [1 ].Value , Equals , tag1 .Value )
2973+ }
2974+
2975+ s .bucket .DeleteObject (objectName )
2976+ s .bucket .DeleteObject (destObjectName )
2977+ }
2978+
2979+ func (s * OssBucketSuite ) TestDeleteObjectTagging (c * C ) {
2980+ // delete object tagging, the object is not exist
2981+ objectName := objectNamePrefix + randStr (8 )
2982+ err := s .bucket .DeleteObjectTagging (objectName )
2983+ c .Assert (err , NotNil )
2984+
2985+ // delete object tagging
2986+ tag := Tag {
2987+ Key : randStr (8 ),
2988+ Value : randStr (16 ),
2989+ }
2990+ tagging := Tagging {
2991+ Tags : []Tag {tag },
2992+ }
2993+ err = s .bucket .PutObject (objectName , strings .NewReader (randStr (1024 )), SetTagging (tagging ))
2994+ c .Assert (err , IsNil )
2995+ err = s .bucket .DeleteObjectTagging (objectName )
2996+ c .Assert (err , IsNil )
2997+ taggingResult , err := s .bucket .GetObjectTagging (objectName )
2998+ c .Assert (err , IsNil )
2999+ c .Assert (len (taggingResult .Tags ), Equals , 0 )
3000+
3001+ //delete object tagging again
3002+ err = s .bucket .DeleteObjectTagging (objectName )
3003+ c .Assert (err , IsNil )
3004+
3005+ s .bucket .DeleteObject (objectName )
3006+ }
0 commit comments