object_tagging.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package sample
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. // ObjectTaggingSample shows how to set and get object Tagging
  8. func ObjectTaggingSample() {
  9. // Create bucket
  10. bucket, err := GetTestBucket(bucketName)
  11. if err != nil {
  12. HandleError(err)
  13. }
  14. // Create object
  15. err = bucket.PutObject(objectKey, strings.NewReader("ObjectTaggingSample"))
  16. if err != nil {
  17. HandleError(err)
  18. }
  19. // Case 1: Set Tagging of object
  20. tag1 := oss.Tag{
  21. Key: "key1",
  22. Value: "value1",
  23. }
  24. tag2 := oss.Tag{
  25. Key: "key2",
  26. Value: "value2",
  27. }
  28. tagging := oss.Tagging{
  29. Tags: []oss.Tag{tag1, tag2},
  30. }
  31. err = bucket.PutObjectTagging(objectKey, tagging)
  32. if err != nil {
  33. HandleError(err)
  34. }
  35. // Case 2: Get Tagging of object
  36. taggingResult, err := bucket.GetObjectTagging(objectKey)
  37. if err != nil {
  38. HandleError(err)
  39. }
  40. fmt.Printf("Object Tagging: %v\n", taggingResult)
  41. tag3 := oss.Tag{
  42. Key: "key3",
  43. Value: "value3",
  44. }
  45. // Case 3: Put object with tagging
  46. tagging = oss.Tagging{
  47. Tags: []oss.Tag{tag1, tag2, tag3},
  48. }
  49. err = bucket.PutObject(objectKey, strings.NewReader("ObjectTaggingSample"), oss.SetTagging(tagging))
  50. if err != nil {
  51. HandleError(err)
  52. }
  53. // Case 4: Delete Tagging of object
  54. err = bucket.DeleteObjectTagging(objectKey)
  55. if err != nil {
  56. HandleError(err)
  57. }
  58. // Delete object and bucket
  59. err = DeleteTestBucketAndObject(bucketName)
  60. if err != nil {
  61. HandleError(err)
  62. }
  63. fmt.Println("ObjectACLSample completed")
  64. }