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 use AmazonS3FullAccess. 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.
packagemainimport("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")typeUploadResultstruct{Pathstring`json:"path" xml:"path"`}funcupload(cecho.Context)error{file,err:=c.FormFile("file")iferr!=nil{returnerr}src,err:=file.Open()iferr!=nil{returnerr}defersrc.Close()result,err:=UploadToS3(c,file.Filename,src)iferr!=nil{returnerr}data:=&UploadResult{Path:result,}returnc.JSON(http.StatusOK,data)}funcUploadToS3(cecho.Context,filenamestring,srcmultipart.File)(string,error){logger:=c.Logger()cfg,err:=config.LoadDefaultConfig(context.TODO())iferr!=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,})iferr!=nil{logger.Fatal(err)return"",err}returnresult.Location,nil}funcmain(){e:=echo.New()e.Use(middleware.Logger())e.GET("/",func(cecho.Context)error{returnc.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.