Resize multipart.File type image with golang ~ Upload to S3


While developing the API server made by golang individually, I implemented the logic to edit the image file received from the front side such as resizing and upload it to S3. At that time, I struggled quite a bit, so I would like to summarize the main points.


Parse multipart/form-data

file, _, err := r.FormFile("image")

It will be possible to handle file as multipart.File type with go.

Pour the contents into the buffer

img := &models.Image{
		UserID: uid,
		Buf:    &bytes.Buffer{},

	_, err := img.Buf.ReadFrom(file)
	if err != nil {
		return &models.Image{}, err

Since this app uses layered architecture + DDD, we declare the Image model as a pointer type and flow the data into the buffer by passing by reference. At this time, if you do not declare the Buf that stores the buffer, the address area will not be secured and a nil error will occur, so be careful.

image editing

func ResizeImage(i *models.Image) error {
img, t, err := image.Decode(i.Buf)
	if err != nil {
		return err


err = jpeg.Encode(i.Buf, m, nil)
		if err != nil {
			return err

If you make it a buffer type, you can decode and encode it, so edit the image such as resizing here.

Upload to S3

uploader := s3manager.NewUploader(sess)
		_, err = uploader.Upload(&s3manager.UploadInput{
			Bucket: aws.String("example"),
			Key:    aws.String(img.Name),
			Body:   img.Buf,

You can upload as a buffer type, so just define the target bucket, name, etc. and you're done! !!

Reference material

Parsing multipart/form-data in Go AWS SDK for Go S3 Bucket Basic Operation ( Impression when creating real-time image resizing API with Go + Serverless Application Model

Recommended Posts

Resize multipart.File type image with golang ~ Upload to S3
Upload images to S3 with GUI using tkinter
Upload the image downloaded by requests directly to S3
Image upload & customization with django-ckeditor
Let's upload S3 files with CLI
Image upload & download to Azure Storage. With Python + requests + REST API
Convert PDF to image with ImageMagick
Use boto3 to mess with S3
Image upload function with Vue.js + Flask
Upload what you got in request to S3 with AWS Lambda Python
HTML email with image to send with python
Image classification with Keras-From preprocessing to classification test-
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Convert array (struct) to json with golang
Connect to s3 with AWS Lambda Python
[Golang] Create docker image with Github Actions
Use boto to upload / download files to s3.
Try to generate an image with aliasing
Upload images to Google Drive with Python
Sample to convert image to Wavelet with Python
How to upload with Heroku, Flask, Python, Git (4)
Upload scraped artifacts in Scrapy Cloud to S3
Crop the image to rounded corners with pythonista
Convert PDF to image (JPEG / PNG) with Python
How to crop an image with Python + OpenCV
Export RDS snapshot to S3 with Lambda (Python)
Upload files to Google Drive with Lambda (Python)
How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
Post an article with an image to WordPress with Python