S3 Image Uploads via AWS-SDK with Golang

An Ultra-Basic GO AWS-SDK example for S3 Bucket image uploads

Define Credentials:

awsAccessKey := "Insert Key Here" 
awsSecret := "Insert Secret Here"
token := ""
creds := credentials.NewStaticCredentials(awsAccessKey, awsSecret, token)
_, err := creds.Get() 
if err != nil {
// handle error
}

Set up Configuration and S3 Instance

cfg := aws.NewConfig().WithRegion("us-west-1").WithCredentials(creds)
svc := s3.New(session.New(), cfg)

Prepping the file

file, err := os.Open("test.jpg") 
if err != nil {
// handle error
}
defer file.Close()
fileInfo, _ := file.Stat() 
var size int64 = fileInfo.Size()
buffer := make([]byte, size) file.Read(buffer) fileBytes := bytes.NewReader(buffer) 
fileType := http.DetectContentType(buffer)
path := "/media/" + file.Name() 
params := &s3.PutObjectInput{
Bucket: aws.String("nameofBucketHere"),
Key: aws.String(path),
Body: fileBytes,
ContentLength: aws.Int64(size),
ContentType: aws.String(fileType),
}
resp, err := svc.PutObject(params) 
if err != nil {
// handle error
}
fmt.Printf("response %s”, awsutil.StringValue(resp))
import( 
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/aws/session"
)
func main() {
aws_access_key_id := "Insert Key ID here"
aws_secret_access_key := "Insert Secret Here"
token := ""
creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, token)
_, err := creds.Get()
if err != nil {
// handle error
}
cfg := aws.NewConfig().WithRegion("us-west-1").WithCredentials(creds) svc := s3.New(session.New(), cfg)

file, err := os.Open("test.jpg")
if err != nil {
// handle error
}
defer file.Close() fileInfo, _ := file.Stat() size := fileInfo.Size() buffer := make([]byte, size) // read file content to buffer

file.Read(buffer)
fileBytes := bytes.NewReader(buffer)
fileType := http.DetectContentType(buffer)
path := "/media/" + file.Name()
params := &s3.PutObjectInput{
Bucket: aws.String("testBucket"),
Key: aws.String(path),
Body: fileBytes,
ContentLength: aws.Int64(size),
ContentType: aws.String(fileType),
}
resp, err := svc.PutObject(params)
if err != nil {
// handle error
}
fmt.Printf("response %s", awsutil.StringValue(resp))
}

Receiving Images from a Form

//r is an *http.Request object
file, h, err := r.FormFile(“data”) // or whatever the form key is
if err != nil {
// handle error
}

--

--

--

Software Architect

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Quest Henkart

Quest Henkart

Software Architect

More from Medium

gRPC Request Routing (Header Based) AWS ALB

Robust media upload with Golang and Cloudinary — Echo Version

Join string using strings.Builder! 6 simple ways to optimise Golang(4)

Golang packages