Skip to content

Instantly share code, notes, and snippets.

@spankie
Last active February 8, 2019 00:19
Show Gist options
  • Save spankie/ec6428c70684e9ad73078ad4cba82cf0 to your computer and use it in GitHub Desktop.
Save spankie/ec6428c70684e9ad73078ad4cba82cf0 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
)
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))
}
package main
import (
"fmt"
"log"
)
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()
}
func main() {
http.HandleFunc("/", handler)
log.Println("Upload server started")
log.Fatal(http.ListenAndServe(":8080", nil))
}
package main
import (
"mime/multipart"
"path/filepath"
"net/http"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/globalsign/mgo/bson"
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment