[Go] Upload a string as a text file to S3

Created on November 30, 2026

You can pass a reader over a string to S3's PutObject operation to upload its contents as a text file. This example uses AWS SDK for Go v1, which reached end of support on July 31, 2025.

package main

import (
    "bytes"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

func main() {
    sess, err := session.NewSession(&aws.Config{Region: aws.String("ap-northeast-1")})
    if err != nil {
        panic(err)
    }
    client := s3.New(sess)

    _, err = client.PutObject(&s3.PutObjectInput{
        Bucket: aws.String("example-bucket"),
        Key:    aws.String("dummy-file-name.txt"),
        Body:   bytes.NewReader([]byte("Hello world.")),
    })
    if err != nil {
        panic(err)
    }
}

Set Bucket to your bucket's name and use the bucket's region and credentials with write access. Key is the object path within the bucket. Body supplies the bytes to upload.

Reference

Go