Created
September 5, 2019 13:46
-
-
Save alexrios/6cbd8b095366bad39108c0de72ec4644 to your computer and use it in GitHub Desktop.
stream 'multipart form data' to S3 bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func MultipartUploadHandler(c *gin.Context) { | |
multipart, err := c.Request.MultipartReader() | |
if err != nil { | |
log.Fatalln("Failed to create MultipartReader", err) | |
} | |
for { | |
mimePart, err := multipart.NextPart() | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
log.Printf("Error reading multipart section: %v", err) | |
break | |
} | |
disposition, params, err := mime.ParseMediaType(mimePart.Header.Get("Content-Disposition")) | |
if err != nil { | |
log.Printf("Invalid Content-Disposition: %v", err) | |
break | |
} | |
// S3 Upload Manager | |
uploader := s3manager.NewUploader(session.New(&aws.Config{Region: aws.String("us-west-2")})) | |
result, err := uploader.Upload(&s3manager.UploadInput{ | |
Body: mimePart, | |
Bucket: aws.String("yourbucket"), | |
Key: aws.String(params["filename"]), | |
ContentType: aws.String(mimePart.Header.Get("Content-Type")), | |
ACL: aws.String("public-read"), | |
}) | |
if err != nil { | |
log.Fatalln("Failed to upload to S3", err) | |
} | |
c.JSON(http.StatusOK, gin.H{ | |
"location": result.Location, | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment