This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bucket.go
146 lines (120 loc) · 3.37 KB
/
bucket.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package aws
import (
"fmt"
amazon_aws "github.com/aws/aws-sdk-go/aws"
amazon_s3 "github.com/aws/aws-sdk-go/service/s3"
)
type Bucket struct {
Name string
Region string
}
func (aws *AWS) NewBucket(bucketName, region string) *Bucket {
svc := aws.getS3()
aws.logger.Println("Looking for existing bucket...")
blo, err := svc.ListBuckets(&amazon_s3.ListBucketsInput{})
if err != nil {
aws.logger.Fatalf("Unable to get list of buckets\n%v\n", err)
}
exists := false
for _, b := range blo.Buckets {
if *b.Name == bucketName {
aws.logger.Println("Bucket found.")
exists = true
break
}
}
if !exists {
return aws.createBucket(bucketName, region)
}
// Tag Bucket
aws.tagBucket(bucketName, map[string]string{
"statica.created-by": "statica",
})
return &Bucket{
Name: bucketName,
Region: region,
}
}
func (b *Bucket) GetHTTPEndpoint() string {
return fmt.Sprintf("%s.s3-website.%s.amazonaws.com", b.Name, b.Region)
}
func (aws *AWS) createBucket(bucketName, region string) *Bucket {
svc := aws.getS3()
var err error
aws.logger.Println("Bucket does not exists yet. Creating...")
_, err = svc.CreateBucket(&amazon_s3.CreateBucketInput{Bucket: amazon_aws.String(bucketName)})
if err != nil {
aws.logger.Fatalf("Unable to create a bucket\n%v\n", err)
}
err = svc.WaitUntilBucketExists(&amazon_s3.HeadBucketInput{Bucket: amazon_aws.String(bucketName)})
if err != nil {
aws.logger.Fatalf("Error occurred while waiting for bucket creation\n%v\n", err)
}
aws.logger.Println("Bucket created")
// Bucket configuration (static website hosting, perimissions, etc.)
aws.logger.Println("Configuring bucket...")
_, err = svc.PutBucketWebsite(&amazon_s3.PutBucketWebsiteInput{
Bucket: amazon_aws.String(bucketName),
WebsiteConfiguration: &amazon_s3.WebsiteConfiguration{
IndexDocument: &amazon_s3.IndexDocument{Suffix: amazon_aws.String("index.html")},
ErrorDocument: &amazon_s3.ErrorDocument{Key: amazon_aws.String("error.html")},
},
})
if err != nil {
aws.logger.Fatalf("Error occurred while configuring bucket (website)\n%v\n", err)
}
_, err = svc.PutBucketPolicy(&amazon_s3.PutBucketPolicyInput{
Bucket: amazon_aws.String(bucketName),
Policy: amazon_aws.String(
fmt.Sprintf(`{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::%s/*"
]
}
]
}`,
bucketName,
),
),
})
if err != nil {
aws.logger.Fatalf("Error occurred while configuring bucket (policy)\n%v\n", err)
}
aws.logger.Println("Bucket configured")
return &Bucket{
Name: bucketName,
Region: region,
}
}
func (aws *AWS) tagBucket(bucketName string, tags map[string]string) {
svc := aws.getS3()
var err error
aws.logger.Println("Tagging bucket...")
var awsTags []*amazon_s3.Tag
for key, val := range tags {
awsTags = append(awsTags, &amazon_s3.Tag{
Key: amazon_aws.String(key),
Value: amazon_aws.String(val),
})
}
putInput := &amazon_s3.PutBucketTaggingInput{
Bucket: amazon_aws.String(bucketName),
Tagging: &amazon_s3.Tagging{
TagSet: awsTags,
},
}
_, err = svc.PutBucketTagging(putInput)
if err != nil {
aws.logger.Fatalf("Error occurred while tagging the bucket\n%v\n", err)
}
aws.logger.Println("Bucket tagged")
}