[Go] Read an S3 object with the AWS SDK for Go v2

Created on November 29, 2026

You can use GetObject on an s3.Client to fetch an object from an S3 bucket. This example uses the default AWS configuration profile and prints the contents of a text object. Replace the bucket and key with values you can access and configure the profile with the bucket's region and credentials that have s3:GetObject permission.

package main

import (
    "context"
    "fmt"
    "io"

    "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() {
    ctx := context.Background()

    cfg, err := config.LoadDefaultConfig(
        ctx,
        config.WithSharedConfigProfile("default"),
    )
    if err != nil {
        panic(err)
    }

    client := s3.NewFromConfig(cfg)
    response, err := client.GetObject(ctx, &s3.GetObjectInput{
        Bucket: aws.String("example-bucket"),
        Key:    aws.String("reports/scores.txt"),
    })
    if err != nil {
        panic(err)
    }
    defer response.Body.Close()

    body, err := io.ReadAll(response.Body)
    if err != nil {
        panic(err)
    }
    fmt.Print(string(body))
}
Go AWS