-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (60 loc) · 1.84 KB
/
main.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
package main
import (
"bytes"
"log"
"net/http"
"os"
"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/gorilla/mux"
)
const (
AWS_S3_REGION = "us-east-2"
AWS_S3_BUCKET = "moviepic"
AKID = "AKIATICND5ZYGYCNGVXY"
SECRET_KEY = "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"
)
func uploadimagetos3(w http.ResponseWriter, r *http.Request) {
//session, err := session.NewSession(&aws.Config{Region: aws.String(AWS_S3_REGION)})
session, err := session.NewSession(&aws.Config{
Region: aws.String(AWS_S3_REGION),
Credentials: credentials.NewStaticCredentials(AKID, SECRET_KEY, ""),
})
if err != nil {
log.Fatal(err)
}
// Upload image
err = uploadFile(session, "test_11zon.jpeg")
if err != nil {
log.Fatal(err)
}
}
func uploadFile(session *session.Session, uploadFileDir string) error {
upFile, err := os.Open(uploadFileDir)
if err != nil {
return err
}
defer upFile.Close()
upFileInfo, _ := upFile.Stat()
var fileSize int64 = upFileInfo.Size()
fileBuffer := make([]byte, fileSize)
upFile.Read(fileBuffer)
_, err = s3.New(session).PutObject(&s3.PutObjectInput{
Bucket: aws.String(AWS_S3_BUCKET),
Key: aws.String(uploadFileDir),
ACL: aws.String("private"),
Body: bytes.NewReader(fileBuffer),
ContentLength: aws.Int64(fileSize),
ContentType: aws.String(http.DetectContentType(fileBuffer)),
ContentDisposition: aws.String("attachment"),
ServerSideEncryption: aws.String("AES256"),
})
return err
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", uploadimagetos3).Methods("PUT")
log.Fatal(http.ListenAndServe("Localhost:5000", r))
}