-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.go
44 lines (35 loc) · 1.03 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
/*
Upload a file to an AWS S3 Bucket
*/
package main
import (
"fmt"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
func handlerUpload(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(10 << 20)
// Get a file from the form input name "file"
file, header, err := r.FormFile("file")
if err != nil {
showError(w, r, http.StatusInternalServerError, "Something went wrong retrieving the file from the form")
return
}
defer file.Close()
filename := header.Filename
// Upload the file to S3.
uploader := s3manager.NewUploader(sess)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(AWS_S3_BUCKET), // Bucket
Key: aws.String(filename), // Name of the file to be saved
Body: file, // File
})
if err != nil {
// Do your error handling here
showError(w, r, http.StatusInternalServerError, "Something went wrong uploading the file")
return
}
fmt.Fprintf(w, "Successfully uploaded to %q\n", AWS_S3_BUCKET)
return
}