-
Notifications
You must be signed in to change notification settings - Fork 6
/
s3.go
155 lines (126 loc) · 3.74 KB
/
s3.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
/*
* Copyright 2016 Frank Wessels <fwessels@xs4all.nl>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package s3
import (
"io"
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/s3git/s3git-go/internal/config"
"time"
)
type Client struct {
Bucket string
Region string
AccessKey string
SecretKey string
Endpoint string
}
func MakeClient(remote config.RemoteObject) *Client {
return &Client{
Bucket: remote.S3Bucket,
Region: remote.S3Region,
AccessKey: remote.S3AccessKey, SecretKey: remote.S3SecretKey,
Endpoint: remote.S3Endpoint}
}
// Upload a file to S3
func (c *Client) UploadWithReader(hash string, r io.Reader) error {
uploader := s3manager.NewUploader(session.New(c.getAwsConfig()))
_, err := uploader.Upload(&s3manager.UploadInput{
Body: r,
Bucket: aws.String(c.Bucket),
Key: aws.String(hash),
})
if err != nil {
return err
}
return nil
}
// Verify the existence of a hash in S3
func (c *Client) VerifyHash(hash string) (bool, error) {
svc := s3.New(session.New(), c.getAwsConfig())
_, err := svc.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(c.Bucket),
Key: aws.String(hash),
})
if err != nil {
if _, ok := err.(awserr.Error); ok {
if reqErr, ok := err.(awserr.RequestFailure); ok {
// A service error occurred
if reqErr.StatusCode() == 404 {
return false, nil
}
}
}
return false, errors.New("Failed to get head of object")
}
return true, nil
}
func (c *Client) DownloadWithWriter(hash string, w io.WriterAt) error {
downloader := s3manager.NewDownloader(session.New(c.getAwsConfig()))
_, err := downloader.Download(w,
&s3.GetObjectInput{
Bucket: aws.String(c.Bucket),
Key: aws.String(hash),
})
if err != nil {
return err
}
return nil
}
// List with a prefix string in S3
func (c *Client) List(prefix string, action func(key string)) ([]string, error) {
lister := lister{Client: c, action: action}
client := s3.New(session.New(), c.getAwsConfig())
params := &s3.ListObjectsInput{Bucket: &c.Bucket, Prefix: &prefix}
client.ListObjectsPages(params, lister.eachPage)
result := make([]string, 0)
return result, nil
}
type lister struct {
*Client
action func(key string)
}
func (l *lister) eachPage(page *s3.ListObjectsOutput, more bool) bool {
for _, obj := range page.Contents {
l.action(*obj.Key)
}
return true
}
func (c *Client) GetPresignedUrl(key string) (string, error) {
svc := s3.New(session.New(), c.getAwsConfig())
req, _ := svc.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(c.Bucket),
Key: aws.String(key),
})
return req.Presign(60 * time.Minute)
}
func (c *Client) getAwsConfig() *aws.Config {
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, ""),
Region: aws.String(c.Region)}
if c.Endpoint != "" {
s3Config.Endpoint = aws.String(c.Endpoint)
s3Config.Region = aws.String("us-east-1") // TODO: Remove hard-coded region for endpoints
s3Config.DisableSSL = aws.Bool(true)
s3Config.S3ForcePathStyle = aws.Bool(true)
}
return s3Config
}