DEV Community

oritsegbubemi
oritsegbubemi

Posted on

How to Upload Images to AWS S3 with Golang

In this tutorial, we will walk through the steps to upload images to AWS S3 using Golang. We'll cover setting up the necessary packages, configuring AWS, and writing the code to handle the image upload. Let's get started!

Prerequisites
Before we begin, ensure you have the following:

. An AWS account with S3 access.
. Go installed on your machine.
. A basic understanding of Go and its ecosystem.

mkdir go_mongoDb
cd go_mongoDb
go mod init go_mongoDb

Next, install the required packages:

go get github.com/aws/aws-sdk-go-v2
go get github.com/aws/aws-sdk-go-v2/config
go get github.com/aws/aws-sdk-go-v2/service/s3
go get github.com/aws/aws-sdk-go-v2/feature/s3/manager
go get github.com/gin-gonic/gin
go get github.com/joho/godotenv
go get go.mongodb.org/mongo-driver/mongo

tep 2: Configuring AWS
Create a .env file in the root of your project and add your AWS credentials:

AWS_REGION=your-aws-region
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key

Step 3: Writing the Upload Code
Create a file named upload.go under a utils package and add the following code:

Image description

Step 4: Integrating with Your Application

Create a controller to handle image uploads. In your controller package, create a file named userController.go and Create service to handle the Bussiness logic, create a file named userService.go:

Service

Image description

Image description

Controller

Image description

Step 5: Setting Up Routes
In your server package, create a server.go file to set up the routes:

Image description

Conclusion
You now have a working Go application that uploads images to AWS S3. This tutorial covered the essential steps to set up AWS configuration, write the upload logic, and integrate it with a web server using the Gin framework.

Feel free to extend this example by adding user authentication, storing image URLs in a database, and more. Happy coding!

get full code here
https://github.com/gbubemi22/goSetup.git

Top comments (0)