-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbucket.go
297 lines (258 loc) · 7.83 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package s3
import (
"bytes"
"fmt"
"io"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
SDK "github.com/aws/aws-sdk-go/service/s3"
"github.com/evalphobia/aws-sdk-go-wrapper/private/pointers"
)
// ACL settings
const (
ACLAuthenticatedRead = "authenticated-read"
ACLPrivate = "private"
ACLPublicRead = "public-read"
ACLPublicReadWrite = "public-read-write"
)
const (
defaultExpireSecond = 180
)
// Bucket is S3 Bucket wrapper struct.
type Bucket struct {
service *S3
name string
nameWithPrefix string
endpoint string
expireSecond int
putSpoolMu sync.Mutex
putSpool []*SDK.PutObjectInput
}
// NewBucket returns initialized *Bucket.
func NewBucket(svc *S3, name string) *Bucket {
bucketName := svc.prefix + name
return &Bucket{
service: svc,
name: name,
nameWithPrefix: bucketName,
endpoint: svc.endpoint,
expireSecond: defaultExpireSecond,
}
}
// SetExpire sets default expire sec for ACL access.
func (b *Bucket) SetExpire(sec int) {
b.expireSecond = sec
}
// AddObject adds object to write spool (w/ public read access).
func (b *Bucket) AddObject(obj *PutObject, path string) {
b.addObject(obj, path, ACLPublicRead)
}
// AddSecretObject adds object to write spool (w/ ACL permission).
func (b *Bucket) AddSecretObject(obj *PutObject, path string) {
b.addObject(obj, path, ACLAuthenticatedRead)
}
// AddPrivateObject adds object to write spool (w/ Private).
func (b *Bucket) AddPrivateObject(obj *PutObject, path string) {
b.addObject(obj, path, ACLPrivate)
}
// addObject adds object to write spool.
func (b *Bucket) addObject(obj *PutObject, path, acl string) {
b.putSpoolMu.Lock()
defer b.putSpoolMu.Unlock()
size := obj.Size()
req := &SDK.PutObjectInput{
ACL: &acl,
Bucket: &b.nameWithPrefix,
Body: obj.data,
ContentLength: &size,
ContentType: pointers.String(obj.FileType()),
Key: pointers.String(path),
}
b.putSpool = append(b.putSpool, req)
}
// PutAll executes PutObject operation in the put spool.
func (b *Bucket) PutAll() error {
b.putSpoolMu.Lock()
defer b.putSpoolMu.Unlock()
errList := newErrors()
cli := b.service.client
for _, obj := range b.putSpool {
_, err := cli.PutObject(obj)
if err != nil {
b.service.Errorf("error on `PutObject` operation; bucket=%s; error=%s;", b.nameWithPrefix, err.Error())
errList.Add(err)
}
}
b.putSpool = nil
if errList.HasError() {
return errList
}
return nil
}
// PutOne executes PutObject operation in the put spool.
func (b *Bucket) PutOne(obj *PutObject, path, acl string) error {
size := obj.Size()
req := &SDK.PutObjectInput{
ACL: &acl,
Bucket: &b.nameWithPrefix,
Body: obj.data,
ContentLength: &size,
ContentType: pointers.String(obj.FileType()),
Key: pointers.String(path),
}
_, err := b.service.client.PutObject(req)
if err != nil {
b.service.Errorf("error on `PutObject` operation; bucket=%s; error=%s;", b.nameWithPrefix, err.Error())
}
return err
}
// GetObjectByte returns bytes of object from given S3 path.
func (b *Bucket) GetObjectByte(path string) ([]byte, error) {
r, err := b.getObject(path)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(r)
if err != nil {
return nil, err
}
return buf.Bytes(), err
}
// GetObjectVersionID returns versionID of object from given S3 path.
func (b *Bucket) GetObjectVersionID(path string) (string, error) {
out, err := b.service.client.GetObject(&SDK.GetObjectInput{
Bucket: &b.nameWithPrefix,
Key: &path,
})
if err != nil {
b.service.Errorf("error on `GetObject` operation; bucket=%s; error=%s;", b.nameWithPrefix, err.Error())
return "", err
}
return aws.StringValue(out.VersionId), nil
}
// getObject fetches object from target S3 path
func (b *Bucket) getObject(path string) (io.Reader, error) {
out, err := b.service.client.GetObject(&SDK.GetObjectInput{
Bucket: &b.nameWithPrefix,
Key: &path,
})
if err != nil {
b.service.Errorf("error on `GetObject` operation; bucket=%s; error=%s;", b.nameWithPrefix, err.Error())
return nil, err
}
return out.Body, nil
}
// GetURL fetches url of target S3 object.
func (b *Bucket) GetURL(path string) string {
return fmt.Sprintf("%s/%s/%s", b.endpoint, b.nameWithPrefix, path)
}
// GetSecretURL fetches a url of target S3 object w/ ACL permission.
func (b *Bucket) GetSecretURL(path string) (string, error) {
return b.GetSecretURLWithExpire(path, b.expireSecond)
}
// GetSecretURLWithExpire fetches a url of target S3 object w/ ACL permission (url expires in `expire` value seconds)
// ** this isn't work **
func (b *Bucket) GetSecretURLWithExpire(path string, expire int) (string, error) {
req, _ := b.service.client.GetObjectRequest(&SDK.GetObjectInput{
Bucket: pointers.String(b.nameWithPrefix),
Key: pointers.String(path),
})
return req.Presign(time.Duration(expire) * time.Second)
}
// HeadObject executes HeadObject operation.
func (b *Bucket) HeadObject(path string) (*SDK.HeadObjectOutput, error) {
return b.service.client.HeadObject(&SDK.HeadObjectInput{
Bucket: pointers.String(b.nameWithPrefix),
Key: pointers.String(path),
})
}
// IsExists checks if an object exists on the given path.
func (b *Bucket) IsExists(path string) bool {
_, err := b.HeadObject(path)
return err == nil
}
// ListAllObjects fetches a list of all of the objects in the bucket and prefix.
func (b *Bucket) ListAllObjects(prefix string) ([]Object, error) {
var contents []Object
nextToken := ""
for {
resp, err := b.ListObjectsV2(ListObjectsRequest{
Prefix: prefix,
ContinuationToken: nextToken,
})
if err != nil {
return contents, err
}
contents = append(contents, resp.Contents...)
if !resp.IsTruncated {
return contents, nil
}
nextToken = resp.NextContinuationToken
}
}
// ListObjectsV2 executes ListObjectsV2 operation.
func (b *Bucket) ListObjectsV2(opt ...ListObjectsRequest) (ListObjectsResponse, error) {
var o ListObjectsRequest
if len(opt) != 0 {
o = opt[0]
}
o.Bucket = b.nameWithPrefix
resp, err := b.service.client.ListObjectsV2(o.ToInput())
return NewListObjectsResponse(resp), err
}
// CopyTo copies an object to destination bucket and path.
func (b *Bucket) CopyTo(srcPath, destBucket, destPath string, opt ...CopyObjectRequest) (CopyObjectResponse, error) {
var o CopyObjectRequest
if len(opt) != 0 {
o = opt[0]
}
bucketName := destBucket
if o.UseSamePrefix {
bucketName = b.service.prefix + bucketName
}
o.SrcBucket = b.nameWithPrefix
o.SrcPath = srcPath
o.DestBucket = bucketName
o.DestPath = destPath
return b.service.CopyObject(o)
}
// CopyFrom copies an object from source buckwt and path.
func (b *Bucket) CopyFrom(srcBucket, srcPath, destPath string, opt ...CopyObjectRequest) (CopyObjectResponse, error) {
var o CopyObjectRequest
if len(opt) != 0 {
o = opt[0]
}
bucketName := srcBucket
if o.UseSamePrefix {
bucketName = b.service.prefix + bucketName
}
o.SrcBucket = bucketName
o.SrcPath = srcPath
o.DestBucket = b.nameWithPrefix
o.DestPath = destPath
return b.service.CopyObject(o)
}
// DeleteObject deletes the object of target path.
func (b *Bucket) DeleteObject(path string) error {
return b.deleteObject(&SDK.DeleteObjectInput{
Bucket: pointers.String(b.nameWithPrefix),
Key: pointers.String(path),
})
}
// DeleteObjectWithVersionID deletes the object of target path and versionId.
func (b *Bucket) DeleteObjectWithVersionID(path string, versionID string) error {
return b.deleteObject(&SDK.DeleteObjectInput{
Bucket: pointers.String(b.nameWithPrefix),
Key: pointers.String(path),
VersionId: pointers.String(versionID),
})
}
func (b *Bucket) deleteObject(in *SDK.DeleteObjectInput) error {
_, err := b.service.client.DeleteObject(in)
if err != nil {
b.service.Errorf("error on `DeleteObject` operation; bucket=%s; error=%s;", b.nameWithPrefix, err.Error())
}
return err
}