This repository was archived by the owner on Sep 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstorageS3.go
93 lines (78 loc) · 2.36 KB
/
storageS3.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
package main
import (
"bytes"
"context"
"fmt"
"io"
"net/url"
"path"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func init() {
registerStorage("s3", newS3Storage)
}
// S3Storage implements a storage option for Amazon S3
type S3Storage struct {
bucket string
path string
conn *s3.S3
}
// NewS3Storage checks config, creates the path and initializes a S3Storage
func newS3Storage(u *url.URL) (storageAdapter, error) {
return &S3Storage{
bucket: u.Host,
path: u.Path,
conn: s3.New(session.New()),
}, nil
}
// Write store the data of a dataObject into the storage
func (s *S3Storage) Write(ctx context.Context, identifier string, data io.Reader) error {
buf := bytes.NewBuffer([]byte{})
io.Copy(buf, data)
s.conn.Config.HTTPClient = getHTTPClient(ctx)
_, err := s.conn.PutObject(&s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Body: bytes.NewReader(buf.Bytes()),
Key: aws.String(path.Join(s.path, identifier)),
})
return err
}
// Read reads the data of a dataObject from the storage
func (s *S3Storage) Read(ctx context.Context, identifier string) (io.Reader, error) {
s.conn.Config.HTTPClient = getHTTPClient(ctx)
out, err := s.conn.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path.Join(s.path, identifier)),
})
if err != nil {
return nil, err
}
return out.Body, nil
}
// IsPresent checks for the presence of an userfile identifier
func (s *S3Storage) IsPresent(ctx context.Context, identifier string) bool {
s.conn.Config.HTTPClient = getHTTPClient(ctx)
out, err := s.conn.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path.Join(s.path, identifier)),
})
if err != nil {
return false
}
return aws.Int64Value(out.ContentLength) > 0 || aws.StringValue(out.ContentType) == "binary/octet-stream"
}
// Backup creates a backup of the old data
func (s *S3Storage) Backup(ctx context.Context, identifier string) error {
ts := strconv.FormatInt(time.Now().Unix(), 10)
s.conn.Config.HTTPClient = getHTTPClient(ctx)
_, err := s.conn.CopyObject(&s3.CopyObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path.Join(s.path, "backup", fmt.Sprintf("%s.%s", identifier, ts))),
CopySource: aws.String(path.Join(s.bucket, s.path, identifier)),
})
return err
}