forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addTags.go
91 lines (78 loc) · 1.93 KB
/
addTags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[extending.go.add_tags]
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"fmt"
)
// Tag S3 bucket MyBucket with cost center tag "123456" and stack tag "MyTestStack".
//
// See:
//
// http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html
func main() {
// Pre-defined values
bucket := "MyBucket"
tagName1 := "Cost Center"
tagValue1 := "123456"
tagName2 := "Stack"
tagValue2 := "MyTestStack"
// Initialize a session in us-west-2 that the SDK will use to load credentials
// from the shared credentials file. (~/.aws/credentials).
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)
if err != nil {
fmt.Println(err.Error())
return
}
// Create S3 service client
svc := s3.New(sess)
// Create input for PutBucket method
putInput := &s3.PutBucketTaggingInput{
Bucket: aws.String(bucket),
Tagging: &s3.Tagging{
TagSet: []*s3.Tag{
{
Key: aws.String(tagName1),
Value: aws.String(tagValue1),
},
{
Key: aws.String(tagName2),
Value: aws.String(tagValue2),
},
},
},
}
_, err = svc.PutBucketTagging(putInput)
if err != nil {
fmt.Println(err.Error())
return
}
// Now show the tags
// Create input for GetBucket method
getInput := &s3.GetBucketTaggingInput{
Bucket: aws.String(bucket),
}
result, err := svc.GetBucketTagging(getInput)
if err != nil {
fmt.Println(err.Error())
return
}
numTags := len(result.TagSet)
if numTags > 0 {
fmt.Println("Found", numTags, "Tag(s):")
fmt.Println("")
for _, t := range result.TagSet {
fmt.Println(" Key: ", *t.Key)
fmt.Println(" Value:", *t.Value)
fmt.Println("")
}
} else {
fmt.Println("Did not find any tags")
}
}
// snippet-end:[extending.go.add_tags]