/
upload.go
107 lines (91 loc) · 2.78 KB
/
upload.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
package main
import (
"bytes"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/cheggaaa/pb"
)
func upload() {
// Create a single AWS session (we can re use this if we're uploading many files)
s, err := session.NewSession(&aws.Config{
Region: aws.String(S3_REGION),
Credentials: credentials.NewStaticCredentials(S3_AKID, S3_SECRET_KEY, S3_TOKEN),
Endpoint: aws.String(S3_ENDPOINT),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(true),
})
if err != nil {
log.Fatal(err)
}
files, err := os.ReadDir(FOLDER_OUT)
if err != nil {
log.Fatal(err)
}
// count all files and multiply them for 4 versions
var count = 0
for _, f := range files {
if _, ok := FILE_TYPE[filepath.Ext(FOLDER_OUT+f.Name())]; ok {
count++
}
}
if count == 0 {
fmt.Println("Sorry the folder \"", FOLDER_OUT, "\" is empty")
return
}
fmt.Println("Resize images ...")
// create and start new bar
bar := pb.StartNew(count)
// start with the resize
for _, f := range files {
if _, ok := FILE_TYPE[filepath.Ext(FOLDER_OUT+f.Name())]; ok {
err = AddFileToS3(s, FOLDER_OUT+f.Name(), f.Name())
if err != nil {
log.Fatal(err)
}
bar.Increment()
}
}
// done
bar.FinishPrint("")
bar.FinishPrint("----------------------------------------")
bar.FinishPrint("Yeah, done! \\o/")
bar.FinishPrint(strconv.Itoa(count) + " Images are uploaded from the folder")
bar.FinishPrint("\"" + FOLDER_OUT + "\" to the s3 bucket")
bar.FinishPrint("----------------------------------------")
}
// AddFileToS3 will upload a single file to S3, it will require a pre-built aws session
// and will set file info like content type and encryption on the uploaded file.
func AddFileToS3(s *session.Session, fileDir string, fileName string) error {
// Open the file for use
file, err := os.Open(fileDir)
if err != nil {
return err
}
defer file.Close()
// Get file size and read the file content into a buffer
fileInfo, _ := file.Stat()
var size int64 = fileInfo.Size()
buffer := make([]byte, size)
file.Read(buffer)
// Config settings: this is where you choose the bucket, filename, content-type etc.
// of the file you're uploading.
_, err = s3.New(s).PutObject(&s3.PutObjectInput{
Bucket: aws.String(S3_BUCKET),
Key: aws.String(fileName),
ACL: aws.String("private"),
Body: bytes.NewReader(buffer),
ContentLength: aws.Int64(size),
ContentType: aws.String(http.DetectContentType(buffer)),
ContentDisposition: aws.String("attachment"),
ServerSideEncryption: aws.String("AES256"),
})
return err
}