forked from buildkite/agent
-
Notifications
You must be signed in to change notification settings - Fork 8
/
s3_uploader.go
140 lines (110 loc) · 3.53 KB
/
s3_uploader.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
package agent
import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/AdRoll/goamz/s3"
"github.com/buildkite/agent/api"
"github.com/buildkite/agent/logger"
"github.com/buildkite/agent/mime"
)
type S3Uploader struct {
// The destination which includes the S3 bucket name
// and the path.
// s3://my-bucket-name/foo/bar
Destination string
// Whether or not HTTP calls shoud be debugged
DebugHTTP bool
// The S3 Bucket we're uploading these files to
Bucket *s3.Bucket
}
func (u *S3Uploader) Setup(destination string, debugHTTP bool) error {
u.Destination = destination
u.DebugHTTP = debugHTTP
// Try to auth with S3
auth, err := awsS3Auth()
if err != nil {
return errors.New(fmt.Sprintf("Error creating AWS S3 authentication: %s", err.Error()))
}
// Try and get the region
region, err := awsS3Region()
if err != nil {
return err
}
logger.Debug("Authorizing S3 credentials and finding bucket `%s` in region `%s`...", u.bucketName(), region.Name)
// Find the bucket
s3 := s3.New(auth, region)
bucket := s3.Bucket(u.bucketName())
// If the list doesn't return an error, then we've got our bucket
_, err = bucket.List("", "", "", 0)
if err != nil {
return errors.New("Could not find bucket `" + u.bucketName() + "` in region `" + region.Name + "` (" + err.Error() + ")")
}
u.Bucket = bucket
return nil
}
func (u *S3Uploader) URL(artifact *api.Artifact) string {
baseUrl := "https://" + u.bucketName() + ".s3.amazonaws.com"
if os.Getenv("BUILDKITE_S3_ACCESS_URL") != "" {
baseUrl = os.Getenv("BUILDKITE_S3_ACCESS_URL")
}
url, _ := url.Parse(baseUrl)
url.Path += u.artifactPath(artifact)
return url.String()
}
func (u *S3Uploader) Upload(artifact *api.Artifact) error {
permission := "public-read"
if os.Getenv("BUILDKITE_S3_ACL") != "" {
permission = os.Getenv("BUILDKITE_S3_ACL")
} else if os.Getenv("AWS_S3_ACL") != "" {
permission = os.Getenv("AWS_S3_ACL")
}
// The dirtiest validation method ever...
if permission != "private" &&
permission != "public-read" &&
permission != "public-read-write" &&
permission != "authenticated-read" &&
permission != "bucket-owner-read" &&
permission != "bucket-owner-full-control" {
logger.Fatal("Invalid S3 ACL `%s`", permission)
}
Perms := s3.ACL(permission)
logger.Debug("Reading file \"%s\"", artifact.AbsolutePath)
data, err := ioutil.ReadFile(artifact.AbsolutePath)
if err != nil {
return errors.New("Failed to read file " + artifact.AbsolutePath + " (" + err.Error() + ")")
}
logger.Debug("Uploading \"%s\" to bucket with permission `%s`", u.artifactPath(artifact), permission)
err = u.Bucket.Put(u.artifactPath(artifact), data, u.mimeType(artifact), Perms, s3.Options{})
if err != nil {
return errors.New(fmt.Sprintf("Failed to PUT file \"%s\" (%s)", u.artifactPath(artifact), err.Error()))
}
return nil
}
func (u *S3Uploader) artifactPath(artifact *api.Artifact) string {
parts := []string{u.bucketPath(), artifact.Path}
return strings.Join(parts, "/")
}
func (u *S3Uploader) bucketPath() string {
return strings.Join(u.destinationParts()[1:len(u.destinationParts())], "/")
}
func (u *S3Uploader) bucketName() string {
return u.destinationParts()[0]
}
func (u *S3Uploader) destinationParts() []string {
trimmed := strings.TrimPrefix(u.Destination, "s3://")
return strings.Split(trimmed, "/")
}
func (u *S3Uploader) mimeType(a *api.Artifact) string {
extension := filepath.Ext(a.Path)
mimeType := mime.TypeByExtension(extension)
if mimeType != "" {
return mimeType
} else {
return "binary/octet-stream"
}
}