forked from spankie/aws-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (80 loc) · 2.64 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"net/http"
"log"
"fmt"
"mime/multipart"
"bytes"
"path/filepath"
"github.com/globalsign/mgo/bson"
"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"
)
// UploadFileToS3 saves a file to aws bucket and returns the url to the file and an error if there's any
func UploadFileToS3(s *session.Session, file multipart.File, fileHeader *multipart.FileHeader) (string, error) {
// get the file size and read
// the file content into a buffer
size := fileHeader.Size
buffer := make([]byte, size)
file.Read(buffer)
// create a unique file name for the file
tempFileName := "pictures/" + bson.NewObjectId().Hex() + filepath.Ext(fileHeader.Filename)
// config settings: this is where you choose the bucket,
// filename, content-type and storage class of the file
// you're uploading
_, err := s3.New(s).PutObject(&s3.PutObjectInput{
Bucket: aws.String("test-bucket"),
Key: aws.String(tempFileName),
ACL: aws.String("public-read"),// could be private if you want it to be access by only authorized users
Body: bytes.NewReader(buffer),
ContentLength: aws.Int64(int64(size)),
ContentType: aws.String(http.DetectContentType(buffer)),
ContentDisposition: aws.String("attachment"),
ServerSideEncryption: aws.String("AES256"),
StorageClass: aws.String("INTELLIGENT_TIERING"),
})
if err != nil {
return "", err
}
return tempFileName, err
}
func handler(w http.ResponseWriter, r *http.Request) {
maxSize := int64(1024000) // allow only 1MB of file size
err := r.ParseMultipartForm(maxSize)
if err != nil {
log.Println(err)
fmt.Fprintf(w, "Image too large. Max Size: %v", maxSize)
return
}
file, fileHeader, err := r.FormFile("profile_picture")
if err != nil {
log.Println(err)
fmt.Fprintf(w, "Could not get uploaded file")
return
}
defer file.Close()
// create an AWS session which can be
// reused if we're uploading many files
s, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-2"),
Credentials: credentials.NewStaticCredentials(
"secret-id", // id
"secret-key", // secret
""), // token can be left blank for now
})
if err != nil {
fmt.Fprintf(w, "Could not upload file")
}
fileName, err := UploadFileToS3(s, file, fileHeader)
if err != nil {
fmt.Fprintf(w, "Could not upload file")
}
fmt.Fprintf(w, "Image uploaded successfully: %v", fileName)
}
func main() {
http.HandleFunc("/", handler)
log.Println("Upload server started")
log.Fatal(http.ListenAndServe(":8080", nil))
}