Ernest Gibbs III
Geek Culture
Published in
6 min readApr 16, 2023

--

Hey Everyone! We’re back to playing around with AWS again because AWS is AWeSome(see what I did there)! This time around we’re doing something different if you’ve been following me you know that we can use Boto3 which is a Python library that works with AWS to automate services but did you know you can do this with other programming languages as well? This is where Golang comes in at.

What’s Golang?

Golang or as it's known by engineers “Go” is a programming language that is compiled, efficient, and open source. Created by Google in 2007, Golang can be used to create web applications, cloud and networking services as well as other different types of software that fit a company’s needs. For transparency, I haven’t had much experience with Go so I’m still learning the ins and outs of this new language.

We know a little bit about what Go is, in this article I’m going to be creating an S3 bucket and uploading files to it using Go. I’ll start by creating my S3 bucket in my AWS management console and then heading over to VSCode and scripting out what I need to upload my files to my S3 bucket. My hope is by the end of the article you’ll know that you can use another programming language to automate AWS services in. Let’s get started!

Objective :

Our coffee company (Go Cafe) needs to store large amounts of data, such as images and videos, for different advertising campaigns. Your team has decided to use AWS S3 to store these files due to its durability, scalability, and cost-effectiveness.

Your task is to develop a Go application that can interact with AWS S3 to upload, download, and delete files. You will need to use the AWS SDK for Go to access S3, which provides a simple interface for interacting with S3 buckets and objects.

Prerequisite/ What We’ll Need:

Golang Knowledge

AWS Knowledge

IDE( I used VSCode)

Determination & Perseverance ALWAYS

Step 1:

Like Boto3, we need to install the necessary packages needed for AWS and Golang to work together. This can be easily researched online along with the documentation for Go SDK for AWS which helped. In this instance, we’re downloading the package for S3 to interact with Go. So let’s head over to VSCode and knock that out.

What the download process looks like.
go get github.com/aws/aws-sdk-go/service/s3

Step 2:

With our installation in place, let’s head over to the AWS Management Console and select “S3” to start the process of creating our bucket. Remember, when setting up your bucket make sure that you have a unique name for the bucket. Also, choose the region where the bucket will be located at as well. I used us-east-1.

In a real-world setting, it may be best practice to set your bucket permissions to private to prevent any unauthorized access to it. By setting our bucket to private it ensures that our data is secured and only to be accessed by authorized individuals or other applications.

By default, S3 buckets are private unless you make the necessary changes to them for the bucket to be public.

Leave your other settings as is and select the “create bucket” option to create the S3 bucket.

Our functional S3 bucket.

Step 3:

Here’s the fun part, we will need to script out the script that will allow us to upload our coffee list and donut pictures to it. The two most important parts of your Golang script which is the package which will be your “package main” command which is responsible for building an executable program. More so, your “import” command is important as that adds more functionality to your program.

Here is a snippet of our code.
package main

import (
"bytes"
"fmt"
"io"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)

// Here, you can choose the region of your bucket
func main() {
region := "us-east-1"

sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
})
if err != nil {
fmt.Println("Error creating session:", err)
return
}
svc := s3.New(sess)

bucket := "golangproject"
filePath := "Donuts.png"

file, err := os.Open(filePath)
if err != nil {
fmt.Fprintln(os.Stderr, "Error opening file:", err)
return
}
defer file.Close()

key := "Donuts.png"

// Read the contents of the file into a buffer
var buf bytes.Buffer
if _, err := io.Copy(&buf, file); err != nil {
fmt.Fprintln(os.Stderr, "Error reading file:", err)
return
}

// This uploads the contents of the buffer to S3
_, err = svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: bytes.NewReader(buf.Bytes()),
})
if err != nil {
fmt.Println("Error uploading file:", err)
return
}

fmt.Println("File uploaded successfully!!!")
}

One of the issues I was running into with my script and getting it to execute was getting the data to write to my S3 bucket. How we resolved this was by adding a buffer argument which is done by adding “bytes.Buffer” to our script. With this, we’re able to read the contents of the file to buffer and it passes it over to be uploaded to S3.

Step 4:

This step is pretty easy as we’ll be adding our content to our bucket. Initially, I got this error when I ran my “go run main.go” command to execute this script.

Make sure that the image or video that you’re trying to upload is in your file so that way Go knows what you’re trying to do. In this case, I only needed to move my image into my project folder for this. Let’s try to run it again.

SUCCESS!

Let’s now add our donuts image.

Both images are added.

Our script works as we were able to upload two images to our S3 bucket through Golang.

Final Thoughts:

This was a fun project to do as I got to learn more about Golang and how it works. Golang has been an interesting programming language that I am still getting used to but I have the desire to get better at it everyday. If you enjoyed reading this be on the lookout for more Golang projects from me in the future and as always feedback is welcomed. See you in the Clouds!

References:

--

--

Ernest Gibbs III
Geek Culture

Follow my journey from Tech Support to Cloud DevOps Engineer! 2x AWS Certified | Linux Certified | Python