DEV Community

Cover image for Let's Get Started with Echo, Go, AWS SDK for GO, and AWS S3

Let's Get Started with Echo, Go, AWS SDK for GO, and AWS S3

Preparation

We need to prepare some tools, such as:

  1. Go. You may download it here. In this post, I use Go 1.18.
  2. Your Favorite IDE (Integrated Development Environment).
  3. 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.

Search S3

  • Click Create bucket

Create Bucket Menu

  • 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.

Create bucket form

  • 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.

New User

  • Fill in the user name and tick the Access key - Programmatic access

Add User Form

  • Click Next. We will setup the permission. For now, we will use AmazonS3FullAccess. You may setup custom permissions to achieve the least privilege.

S3 Permission

  • You may configure other settings and create a user.

  • After that, copy and save the credentials. You will use this later.

Credentials

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
Enter fullscreen mode Exit fullscreen mode
  • 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"))
}
Enter fullscreen mode Exit fullscreen mode
  • 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>
Enter fullscreen mode Exit fullscreen mode

(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>
Enter fullscreen mode Exit fullscreen mode
  • Run our server. go run server.go.

Server Running

  • Call our API with Postman. Make sure you fill correct address bar, HTTP method, and body. After that click Send.

API Call

  • If successful, you will get a message like this in the console.

Success

  • Open S3. Make sure your file is in there.

Bucket Result

Github Project

explore-go

Blog Post

Part of this post: Dev.to

License

BSD 3-Clause

Awesome

You are awesome!

Awesome GIF

GIF Image Source: https://i.giphy.com/media/vCKC987OpQAco/giphy.gif

If you have any questions or problems, feel free to ask for help.

Thank you.

Discussion (0)