OSDN Git Service

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