Uploading Files to Amazon S3 with Go
A Beginner's Guide to File Uploads with Go and AWS S3
In this blog post, I'll guide you through a beginner-friendly project demonstrating how to use the Go programming language and the Amazon Web Services (AWS) SDK to upload files to an AWS S3 bucket.
Here is a video about this topic π
Prerequisites π
Before I get started, make sure you have the following:
Install Go on your machine.
An AWS account and access to an S3 bucket where you can upload files.
The AWS CLI should be installed and configured with your AWS credentials.
The AWS SDK for Go should be installed in your Go workspace.
If you need more detailed information about these prerequisites, please refer to the previous sections of this blog.
Step-by-Step Guideπͺ
Step 1: Import Necessary Packages π¦
The first step in our Go program is to import the necessary packages. This includes the standard Go packages for context handling, formatted I/O, logging, and file system operations. I also need to import parts of the AWS SDK for Go that provide functionality for AWS service configuration and S3 operations:
import (
"context"
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
Step 2: Load AWS SDK Configuration ποΈ
In the main
function, I start by loading the AWS SDK configuration. I specify the AWS region I want to use (in this case, ap-south-1
). If there's an error loading the configuration, I log the error and terminate the program:
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("ap-south-1"))
if err != nil {
log.Fatal("failed to load SDK %v", err)
}
Step 3: Create an S3 Client π€
Next, I create an S3 client using the loaded AWS SDK configuration:
client := s3.NewFromConfig(cfg)
Step 4: Upload a File π
Then, I call our UploadFile
function, passing the S3 client, the name of the S3 bucket, and the name of the file I want to upload:
err = UploadFile(client, "your-bucket-name", "filename")
if err != nil {
log.Fatal("failed to upload file %v", err)
}
If the file is successfully uploaded, a success message is printed:
fmt.Println("file is successfully uploaded")
Step 5: Define the UploadFile
Function π€
Finally, I need to define our UploadFile
function. This function opens the specified file, prepares a PutObjectInput
request to upload the file to S3, and then calls PutObject
on the S3 client to upload the file:
func UploadFile(client *s3.Client, bucket, filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
input := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(filename),
Body: file,
}
_, err = client.PutObject(context.TODO(), input)
return err
}
Wrapping Up π₯
And that's it! If you've followed along, you've created a simple Go program that uploads a file to an Amazon S3 bucket using the AWS SDK for Go.
While this is a basic example, it provides a foundation that you can build upon. You could expand this program to handle multiple files, add error checking and retry logic, and handle large files with
You can Buy Me a Coffee if you want to and please don't forget to follow me on YouTube, Twitter, and LinkedIn also.
If you have any questions or would like to share your own experiences, feel free to leave a comment below. I'm here to support and engage with you.
Happy building!