Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Siddhant-K-code/e0c30bdf6c476b110c910dddc44f44ee to your computer and use it in GitHub Desktop.
Save Siddhant-K-code/e0c30bdf6c476b110c910dddc44f44ee to your computer and use it in GitHub Desktop.
Upload and Download AWS S3 Objects in Go

Upload and Download AWS S3 Objects in Go

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.

Prerequisites

Before running the code, install the AWS SDK for Go using the following command:

$ go get github.com/aws/aws-sdk-go

Upload a File to S3

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)
	}
}

Download a File from S3

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