-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
81 lines (67 loc) · 2.05 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
package main
import (
"bytes"
"image/jpeg"
"io"
"mime/multipart"
"net/http"
"path/filepath"
"strconv"
"time"
"github.com/nfnt/resize"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func NewAWSSession(region, auth, secret string) (*session.Session, error) {
return session.NewSession(&aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(
auth,
secret,
"",
),
})
}
func UploadFileToS3(s *session.Session, file []byte, name string) (string, error) {
bucket := config("S3_BUCKET")
region := *s.Config.Region
timestamp := strconv.Itoa(int(time.Now().Unix()))
fileName := filepath.Base(name) + "." + timestamp + filepath.Ext(name)
maxAgeOneYear := aws.String("max-age=31536000")
_, err := s3.New(s).PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(fileName),
ACL: aws.String("public-read"),
Body: bytes.NewReader(file),
ContentType: aws.String(http.DetectContentType(file)),
ContentDisposition: aws.String("attachment"),
StorageClass: aws.String("STANDARD"),
Metadata: map[string]*string{"Cache-Control": maxAgeOneYear}, // 1 year
})
if err != nil {
return "", err
}
return "https://" + bucket + ".s3." + region + ".amazonaws.com/" + fileName, nil
}
// ResizeJPG takes a multipart.File that is expected to be a jpg
// and the width to resize it to in pixels.
// It returns a []byte containing a resized version of the file
// or an error.
func ResizeJPG(file multipart.File, maxWidth uint) ([]byte, error) {
srcBuf := new(bytes.Buffer)
if _, err := io.Copy(srcBuf, file); err != nil {
return nil, err
}
srcImg, err := jpeg.Decode(srcBuf)
if err != nil {
return nil, err
}
dstImg := resize.Resize(maxWidth, 0, srcImg, resize.Lanczos3)
dstBuf := new(bytes.Buffer)
if err := jpeg.Encode(dstBuf, dstImg, nil); err != nil {
return nil, err
}
return dstBuf.Bytes(), nil
}