This Gist demonstrates how to use the aws-sdk-go
package to upload and download objects from AWS S3 using Go. It utilizes the s3manager
package to simplify these operations.
Before running the code, install the AWS SDK for Go using the following command:
$ go get github.com/aws/aws-sdk-go
This Go program uploads data to an AWS S3 bucket using s3manager.Uploader
.
package main
import (
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
func main() {
// Initialize an Uploader
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String("us-east-1")},
Profile: "default",
})
if err != nil {
panic(err)
}
uploader := s3manager.NewUploader(sess)
// Prepare the data to upload
data := strings.NewReader(`{"message": "hello world"}`)
// Specify the bucket and object path
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String("bucket-name"),
Body: aws.ReadSeekCloser(data),
Key: aws.String("path/to/file"),
ContentType: aws.String("application/json"),
})
if err != nil {
panic(err)
}
}
This Go program downloads an object from AWS S3 using s3manager.Downloader
.
package main
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
func main() {
// Initialize a Downloader
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String("us-east-1")},
Profile: "default",
})
if err != nil {
panic(err)
}
downloader := s3manager.NewDownloader(sess)
// Prepare the write buffer
buf := aws.NewWriteAtBuffer([]byte{})
// Specify the bucket and object path
_, err = downloader.Download(buf, &s3.GetObjectInput{
Bucket: aws.String("bucket-name"),
Key: aws.String("path/to/file"),
})
if err != nil {
panic(err)
}
// Read the content from the buffer
data := bytes.NewBuffer(buf.Bytes()).String()
fmt.Println(data)
}