iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😸

Interacting with LocalStack S3 using Go

に公開

Introduction

In the previous article, I introduced how to interact with LocalStack from Go.
In this article, I will explain the source code for operating S3 on LocalStack that was featured there.

Code Explanation

Re-posting the Source Code

Below is the Go code introduced in the previous article.

package main

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"
)

func main() {
	// Dummy credentials
	os.Setenv("AWS_ACCESS_KEY_ID", "test")
	os.Setenv("AWS_SECRET_ACCESS_KEY", "test")

	cfg, err := config.LoadDefaultConfig(context.TODO(),
		config.WithRegion("us-east-1"),
	)
	if err != nil {
		log.Fatalf("failed to load config, %v", err)
	}

	client := s3.NewFromConfig(cfg, func(o *s3.Options) {
		o.BaseEndpoint = aws.String("http://localhost:4566")
		o.UsePathStyle = true
	})

	buckets, err := client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
	if err != nil {
		log.Fatalf("failed to list buckets, %v", err)
	}

	for _, bucket := range buckets.Buckets {
		fmt.Println(*bucket.Name)
	}
}

Explanation of the Source Code

1. Setting Environment Variables

os.Setenv("AWS_ACCESS_KEY_ID", "test")
os.Setenv("AWS_SECRET_ACCESS_KEY", "test")
  • Since LocalStack does not require credentials, dummy values (test) are set as environment variables.

2. AWS SDK Configuration

cfg, err := config.LoadDefaultConfig(context.TODO(),
	config.WithRegion("us-east-1"),
)
  • Loads the AWS SDK configuration using config.LoadDefaultConfig.
  • context.TODO() is a context used for functions that require a context when the appropriate context has not yet been determined. Since no specific context is decided in this instance, context.TODO() is used.
  • While WithRegion("us-east-1") is specified, this setting does not have any particular impact in LocalStack. It is also fine to set it to something like test.

3. Creating the S3 Client

client := s3.NewFromConfig(cfg, func(o *s3.Options) {
	o.BaseEndpoint = aws.String("http://localhost:4566")
	o.UsePathStyle = true
})
  • By specifying http://localhost:4566 in BaseEndpoint, you can use the LocalStack S3 endpoint.
  • When connecting to S3 on LocalStack, the bucket URL must be in the format http://localhost:4566/{bucket_name}. To use this format, you need to set UsePathStyle = true.
    • Without this setting, the endpoint becomes https://{bucket_name}.s3.amazonaws.com, which prevents connection to S3 on LocalStack.

4. Retrieving the List of Buckets

buckets, err := client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
  • ListBuckets is a method for retrieving all buckets in S3.
  • ListBucketsInput{} is the input parameter for the request, but since no specific configuration is needed, an empty struct is passed.
  • If no buckets exist, buckets.Buckets will be an empty slice.

5. Outputting Bucket Names

for _, bucket := range buckets.Buckets {
	fmt.Println(*bucket.Name)
}
  • buckets.Buckets contains the list of retrieved buckets, and the bucket names are displayed by iterating through the loop.

Discussion