Skip to content

Instantly share code, notes, and snippets.

@hisanari
Last active June 26, 2022 05:55
Show Gist options
  • Save hisanari/b21c294de244018fa3f160c1f4d0f3a5 to your computer and use it in GitHub Desktop.
Save hisanari/b21c294de244018fa3f160c1f4d0f3a5 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"log"
"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() {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"))
if err != nil {
log.Fatal(err)
}
client := s3.NewFromConfig(cfg)
output, err := client.ListObjectsV2(
context.TODO(),
&s3.ListObjectsV2Input{
Bucket: aws.String("バケットの名前"),
})
if err != nil {
log.Fatal(err)
}
log.Println("result")
log.Printf("count %d", len(output.Contents))
for _, object := range output.Contents {
log.Printf("key=%s size=%d", aws.ToString(object.Key), object.Size)
}
}

事前

AWS CLIとかでaws configureの設定をしておく。

goで必要なモジュールをinstallしておく

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment