-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
130 lines (103 loc) · 2.66 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
package main
import (
"fmt"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/s3"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"time"
)
func (s *S3pal) getBucket() *s3.Bucket {
auth, err := aws.GetAuth(s.Config.Aws.AccessKey, s.Config.Aws.SecretKey)
if err != nil {
panic("Could not connect to S3 with your credentials.")
}
client := s3.New(auth, aws.Regions[s.Config.Aws.Region])
bucket := client.Bucket(s.Config.Aws.Bucket)
return bucket
}
func (s *S3pal) uploadToS3(path string, contentType string, filename string) (err error) {
fd, err := os.Open(path)
if err != nil {
log.Printf("Error opening temp: %v", err)
return err
}
defer fd.Close()
if len(contentType) == 0 {
contentType = "binary/octet-stream"
}
bucket := s.getBucket()
bytes, readErr := ioutil.ReadAll(fd)
if readErr != nil {
return readErr
}
headers := map[string][]string{
"Content-Type": []string{contentType},
}
for key, value := range s.Config.Aws.UploadHeaders {
headers[key] = []string{value}
}
err = bucket.PutHeader(filename, bytes, headers, s3.ACL(s.Config.Aws.ACL))
if err != nil {
log.Printf("Error: %v\n", err)
return err
} else {
fmt.Printf("Uploaded %s\n", s.makeUrl(filename))
}
return nil
}
func (s *S3pal) listS3Bucket(prefix string, urls bool, doSign bool, signTTL int64) ([]string, error) {
bucket := s.getBucket()
listresp, err := bucket.List(prefix, "", "", 0)
var result []string
if err != nil {
return result, err
}
now := time.Now()
ttl := time.Duration(signTTL) * time.Second
signExpires := now.Add(ttl)
if err != nil {
log.Printf("Error: %v\n", err)
} else {
for _, obj := range listresp.Contents {
if len(prefix) == 0 || (len(prefix) > 0 && strings.HasPrefix(obj.Key, prefix)) {
if urls {
if doSign {
result = append(result, bucket.SignedURL(obj.Key, signExpires))
} else {
result = append(result, bucket.URL(obj.Key))
}
} else {
result = append(result, obj.Key)
}
}
}
}
return result, nil
}
func (s *S3pal) uploadPathOrURL(filePath string, prefix string) (string, error) {
fmt.Printf("\nUploading '%s' to S3 Bucket '%s'...\n", filePath, s.Config.Aws.Bucket)
var toUploadPath string
_, err := os.Stat(filePath)
if err == nil {
f, err := os.Open(filePath)
if err != nil {
return "", err
}
toUploadPath = f.Name()
} else {
toUploadPath, err = downloadURL(filePath)
if err != nil {
return "", err
}
}
bytes, err := ioutil.ReadFile(toUploadPath)
contentType := http.DetectContentType(bytes)
newFilename := s.makeFilename(prefix, path.Base(filePath))
err = s.uploadToS3(toUploadPath, contentType, newFilename)
return newFilename, err
}