S3 Image Uploads via AWS-SDK with Golang

Uploading files and varying types of media can always be a headache. I was recently tasked with building out a media service; a dedicated Go service that would handle uploads to an s3 bucket. The documentation isn’t completely clear and most of the examples and tutorials are outdated, so I am posting a small snippet of basic working code using the AWS SDK

An Ultra-Basic GO AWS-SDK example for S3 Bucket image uploads

Define Credentials:

The first step to setting up the AWS-SDK is to define your credentials. Eventually you’ll want to do this using an environment variable or the /.aws/credentials file. For now we are going to use static credentials in code Do Not Upload To GitHub or use in Production or Development. Your Secret is like a password

Next we want to set our credentials into a credentials struct provided by the AWS-SDK. In this case we are using NewStaticCredentials, but only because we have statically set credentials, you might instead use NewEnvCredentials to pull in an environment variable

Next we want to call the .Get() method on our credentials struct. Get returns the credentials value or an error if the value cannot be retrieved. In this case, we don’t need the credential’s value, we just need to expose an error if there is one

Set up Configuration and S3 Instance

Next we need to set up a configuration object to pass to the SDK. In this case, we are going to use aws.NewConfig(), which returns a new config file to allow method chaining. We are going to chain this with our region and credentials

Next we need to create a new s3 instance that contains the client and session. s3.New() takes in two parameters, a ConfigProvider and an aws.Config struct

Prepping the file

Next we will prep the file. We need to convert it to []bytes and preferably pass along some metadata

First we will open the file (for this example to work, you’ll need to pass in a path to an actual file)

next we will determine the size of the file

and create a buffer of the size of the file, set the file content to the buffer and then convert it to Bytes

Finally we make the set the path and put together the s3 request

and submit it to AWS


Receiving Images from a Form

A commenter recently asked about how to handle a more real world example where an image or piece of media is sent from a form. Luckily, it’s event easier because the data from the multipart request is already a buffer. You can use an abstracted GO method here

From docs: https://golang.org/pkg/net/http/#Request.FormFile returns the first file for the provided form key. FormFile calls ParseMultipartForm and ParseForm if necessary.

the first return value (file) is the buffer, and the second return value (h) consists of several metadata fields (e.g. h.Header[“Content-Type”][0] will return the content-type, h.Filename will return the filename)

Originally published at questhenkart.com on February 3, 2016.

Software Architect

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store