-
Notifications
You must be signed in to change notification settings - Fork 6
/
bucket.go
139 lines (117 loc) · 2.75 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
package s3
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"time"
)
const (
PrivateACL = "private"
PublicReadACL = "public-read"
PublicReadWriteACL = "public-read-write"
AWSExecReadACL = "aws-exec-read"
AuthenticatedReadACL = "authenticated-read"
BucketOwnerReadACL = "bucket-owner-read"
BucketOwnerFullControlACL = "bucket-owner-full-control"
LogDeliveryWriteACL = "log-delivery-write"
)
func (c *Client) CreateBucket(name, region, acl string) error {
/* validate that the bucket name is:
- between 3 and 63 characters long (inclusive)
- not include periods (for TLS wildcard matching)
- lower case
- rfc952 compliant
*/
if ok, _ := regexp.MatchString(`^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$`, name); !ok {
return fmt.Errorf("invalid s3 bucket name")
}
was := c.Bucket
defer func() { c.Bucket = was }()
c.Bucket = name
b := []byte{}
if region != "" {
var payload struct {
XMLName xml.Name `xml:"CreateBucketConfiguration"`
Region string `xml:"LocationConstraint"`
}
payload.Region = region
var err error
b, err = xml.Marshal(payload)
if err != nil {
return err
}
}
headers := make(http.Header)
headers.Set("x-amz-acl", acl)
res, err := c.put("/", b, &headers)
if err != nil {
return err
}
if res.StatusCode != 200 {
return ResponseError(res)
}
return nil
}
func (c *Client) DeleteBucket(name string) error {
was := c.Bucket
defer func() { c.Bucket = was }()
c.Bucket = name
res, err := c.delete("/", nil)
if err != nil {
return err
}
if res.StatusCode != 204 {
return ResponseError(res)
}
return nil
}
type Bucket struct {
Name string
CreationDate time.Time
OwnerID string
OwnerName string
}
func (c *Client) ListBuckets() ([]Bucket, error) {
prev := c.Bucket
c.Bucket = ""
res, err := c.get("/", nil)
c.Bucket = prev
if err != nil {
return nil, err
}
var r struct {
XMLName xml.Name `xml:"ListAllMyBucketsResult"`
Owner struct {
ID string `xml:"ID"`
DisplayName string `xml:"DisplayName"`
} `xml:"Owner"`
Buckets struct {
Bucket []struct {
Name string `xml:"Name"`
CreationDate string `xml:"CreationDate"`
} `xml:"Bucket"`
} `xml:"Buckets"`
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, ResponseErrorFrom(b)
}
err = xml.Unmarshal(b, &r)
if err != nil {
return nil, err
}
s := make([]Bucket, len(r.Buckets.Bucket))
for i, bkt := range r.Buckets.Bucket {
s[i].OwnerID = r.Owner.ID
s[i].OwnerName = r.Owner.DisplayName
s[i].Name = bkt.Name
created, _ := time.Parse("2006-01-02T15:04:05.000Z", bkt.CreationDate)
s[i].CreationDate = created
}
return s, nil
}