#S3 #AWS #Go言語 #LocalStack [[AWS SDK for Go V2]]で特定キーのObjectを取得する方法。 `your-bucket-name`バケット配下が以下の構造になっているとする。 ```ls - your-bucket-name (バケット名) - path-from-bucket-root ``` 以下のコードで取得できる。 ```go output, err := client.GetObject(context.TODO(), &s3.GetObjectInput{ Bucket: aws.String("your-bucket-name"), Key: aws.String("path-from-bucket-root"), }) if err != nil { log.Fatal(err) } ``` `output.Body`は[[io.ReadCloser]]になっているので、様々なライブラリの変換に対応できる。データが[[JSON]]形式で構造体に変換する場合は以下のようになる。 ```go type Response struct { // 省略 } var res Response decoder := json.NewDecoder(output.Body) err = decoder.Decode(&res) if err != nil { log.Fatal(err) } ``` ## 参考 - [Using the AWS SDK for Go V2 with AWS Services \| AWS SDK for Go V2](https://aws.github.io/aws-sdk-go-v2/docs/making-requests/#responses-with-ioreadcloser) - [Go言語\(golang\)でS3からファイルを取得する \| DevelopersIO](https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2021/07/golang-logo_1200x630-960x504.png)