Preparation
We need to prepare some tools, such as:
- Go. You may download it here. In this post, I use Go 1.18.
- Your Favorite IDE (Integrated Development Environment).
- Postman. You may download here. Feel free to use other similar tools.
Setup AWS S3 and IAM User
- Open AWS Console
- Search S3 in the search bar. Click the S3.
- Click
Create bucket
- Fill in the bucket name and select AWS region. After that, click
Create bucket
.
Note: We let the config as it is. You may configure it.
Go to IAM -> Users.
Create a new user. We will create a user that will have a responsibility to upload the files. Click
Add User
.
- Fill in the user name and tick the
Access key - Programmatic access
- Click
Next
. We will setup the permission. For now, we will useAmazonS3FullAccess
. You may setup custom permissions to achieve the least privilege.
You may configure other settings and create a user.
After that, copy and save the credentials. You will use this later.
Time to Code
Prepare a folder/directory for your project.
Open the terminal (make sure the current directory is your project). Initiate go module. Run
go mod init <project name>
, example:go mod init explore-go
.Add our dependencies. First I add Echo.
go get github.com/labstack/echo/v4
Add AWS SDK and S3 service.
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
- Create file
server.go
with code below. Don't forget to change the bucket name.
package main
import (
"context"
"mime/multipart"
"net/http"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type UploadResult struct {
Path string `json:"path" xml:"path"`
}
func upload(c echo.Context) error {
file, err := c.FormFile("file")
if err != nil {
return err
}
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
result, err := UploadToS3(c, file.Filename, src)
if err != nil {
return err
}
data := &UploadResult{
Path: result,
}
return c.JSON(http.StatusOK, data)
}
func UploadToS3(c echo.Context, filename string, src multipart.File) (string, error) {
logger := c.Logger()
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
logger.Fatal(err)
return "", err
}
client := s3.NewFromConfig(cfg)
uploader := manager.NewUploader(client)
result, err := uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String("<change this with your bucket name>"),
Key: aws.String(filename),
Body: src,
})
if err != nil {
logger.Fatal(err)
return "", err
}
return result.Location, nil
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.POST("/upload", upload)
e.Logger.Fatal(e.Start(":1323"))
}
- Setup environment variable.
(For Linux)
export AWS_REGION=<change with your region>
export AWS_ACCESS_KEY_ID=<change with your key id>
export AWS_SECRET_ACCESS_KEY=<change with you access key>
You can use any name but I recommend the name of the file .env.sh
. After that run the file using source .env.sh
.
(For Windows)
set AWS_REGION=<change with your region>
set AWS_ACCESS_KEY_ID=<change with your key id>
set AWS_SECRET_ACCESS_KEY=<change with you access key>
You can use any name but I recommend the name of the file env.bat
. After that run the file using env.bat
.
- Run our server.
go run server.go
.
- Call our API with Postman. Make sure you fill correct address bar, HTTP method, and body. After that click Send.
- If successful, you will get a message like this in the console.
- Open S3. Make sure your file is in there.
Github Project
berviantoleo / explore-go
Exploring Go
Explore Go (explore-go)
Blog Post
Part of this post: Dev.to
Development
Prepare Dependencies
go get
go install
Run
go run server.go
Build
go build
Test
go test ./...
Test and Check Coverage
go test ./... -covermode=atomic -coverprofile cover.out
License
BSD 3-Clause
Awesome
You are awesome!
If you have any questions or problems, feel free to ask for help.
Thank you.
Top comments (5)
Hi, I tried the same code as yours and I got error saying that "api error AccessDenied: Access Denied\". Do you know why?
Have you set the environment variable? I also need to know which line triggers your error.
Ah it's okay, I figured it out now, I put " " at each of my key in .env. Just removed the " " and everything goes well. Tq !
where do we save the environment variable and what is the filename like, thanks you
in the root of the project, same directory with
server.go
.the filename:
.env.sh
env.bat