Skip to content

Commit e46aae3

Browse files
committed
Initial commit 🤠
0 parents  commit e46aae3

File tree

8 files changed

+192
-0
lines changed

8 files changed

+192
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
debug

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# buffer-static-upload
2+
3+
A straightforward static asset uploader

glide.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package: .
2+
import:
3+
- package: github.com/aws/aws-sdk-go
4+
version: ~1.8.29

main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
7+
"github.com/bufferapp/buffer-static-upload/utils"
8+
)
9+
10+
func main() {
11+
s3Bucket := "buffer-dan-test"
12+
13+
filesArg := flag.String("files", "", "the path to the files you'd like to upload")
14+
flag.Parse()
15+
16+
files, err := utils.GetFilesFromGlobsList(*filesArg)
17+
if err != nil {
18+
fmt.Printf("err %s", err)
19+
}
20+
fmt.Printf("Found %d files to upload and version\n", len(files))
21+
22+
fileVersions, err := utils.VersionAndUploadFiles(s3Bucket, files)
23+
if err != nil {
24+
fmt.Printf("Failed to upload files: %s", err)
25+
}
26+
27+
for file, version := range fileVersions {
28+
fmt.Printf("%s = %s\n", file, version)
29+
}
30+
}

tests/hash-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a dummy file

tests/utils_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package utils
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/bufferapp/buffer-static-upload/utils"
8+
)
9+
10+
func TestGetFileMd5(t *testing.T) {
11+
file, _ := os.Open("hash-test.txt")
12+
hash, _ := utils.GetFileMd5(file)
13+
expected := "bde181f4a40d6e56662537d4e643a487"
14+
if hash != expected {
15+
t.Error("Expected", expected, "got", hash)
16+
}
17+
}

utils/utils.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package utils
2+
3+
import (
4+
"crypto/md5"
5+
"encoding/hex"
6+
"fmt"
7+
"io"
8+
"mime"
9+
"os"
10+
"path/filepath"
11+
"strings"
12+
"time"
13+
14+
"github.com/aws/aws-sdk-go/aws"
15+
"github.com/aws/aws-sdk-go/aws/credentials"
16+
"github.com/aws/aws-sdk-go/aws/endpoints"
17+
"github.com/aws/aws-sdk-go/aws/session"
18+
"github.com/aws/aws-sdk-go/service/s3/s3manager"
19+
)
20+
21+
// GetFileMd5 returns a checksum for a given file
22+
func GetFileMd5(file *os.File) (string, error) {
23+
var fileHash string
24+
hash := md5.New()
25+
if _, err := io.Copy(hash, file); err != nil {
26+
return fileHash, err
27+
}
28+
hashInBytes := hash.Sum(nil)[:16]
29+
fileHash = hex.EncodeToString(hashInBytes)
30+
return fileHash, nil
31+
}
32+
33+
// GetVersionedFilename returns a new filename with the version before the extension
34+
func GetVersionedFilename(filename string, version string) string {
35+
ext := filepath.Ext(filename)
36+
versionedExt := "." + version + ext
37+
versionedFilename := strings.Replace(filename, ext, versionedExt, 1)
38+
return versionedFilename
39+
}
40+
41+
// GetFileMimeType returns the mime type of a file using it's extension
42+
func GetFileMimeType(filename string) string {
43+
ext := filepath.Ext(filename)
44+
return mime.TypeByExtension(ext)
45+
}
46+
47+
// GetFilesFromGlobsList returns a list of files that match a list of
48+
// comma-deliniated file path globs
49+
func GetFilesFromGlobsList(globList string) ([]string, error) {
50+
var files []string
51+
globs := strings.Split(globList, ",")
52+
53+
for _, glob := range globs {
54+
fileList, err := filepath.Glob(glob)
55+
if err != nil {
56+
return files, err
57+
}
58+
files = append(files, fileList...)
59+
}
60+
return files, nil
61+
}
62+
63+
// GetS3Uploader returns a configured Uploader
64+
func GetS3Uploader() (*s3manager.Uploader, error) {
65+
var uploader *s3manager.Uploader
66+
67+
awsAccessKeyID := os.Getenv("AWS_ACCESS_KEY_ID")
68+
awsSecretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
69+
70+
creds := credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, "")
71+
72+
sess := session.Must(session.NewSession(&aws.Config{
73+
Credentials: creds,
74+
Region: aws.String(endpoints.UsEast1RegionID),
75+
}))
76+
77+
_, err := creds.Get()
78+
if err != nil {
79+
fmt.Printf("Bad credentials: %s", err)
80+
return uploader, err
81+
}
82+
83+
uploader = s3manager.NewUploader(sess)
84+
return uploader, nil
85+
}
86+
87+
// VersionAndUploadFiles will verion files and upload them to s3 and return
88+
// a map of filenames and their version hashes
89+
func VersionAndUploadFiles(bucket string, filenames []string) (map[string]string, error) {
90+
fileVersions := map[string]string{}
91+
92+
uploader, err := GetS3Uploader()
93+
if err != nil {
94+
return fileVersions, err
95+
}
96+
97+
for _, filename := range filenames {
98+
file, err := os.Open(filename)
99+
if err != nil {
100+
return fileVersions, err
101+
}
102+
defer file.Close()
103+
104+
checksum, err := GetFileMd5(file)
105+
if err != nil {
106+
return fileVersions, err
107+
}
108+
109+
fileVersions[filename] = checksum
110+
versionedFilename := GetVersionedFilename(filename, checksum)
111+
mimeType := GetFileMimeType(filename)
112+
113+
result, err := uploader.Upload(&s3manager.UploadInput{
114+
Bucket: aws.String(bucket),
115+
Key: aws.String(versionedFilename),
116+
ContentType: aws.String(mimeType),
117+
CacheControl: aws.String("public, max-age=31520626"),
118+
Expires: aws.Time(time.Now().AddDate(10, 0, 0)),
119+
Body: file,
120+
})
121+
if err != nil {
122+
return fileVersions, err
123+
}
124+
125+
fmt.Printf("Uploaded %s\n", result.Location)
126+
}
127+
128+
return fileVersions, nil
129+
}

0 commit comments

Comments
 (0)