Golang : Upload to S3 with official aws-sdk-go package
An update to my previous tutorial on how to upload data to AWS S3, this tutorial uses the "official" AWS-SDK-GO package. As usual, please take note the the AWS-SDK-GO is still underdevelopment and the code example below might become obsolete. At the time of writing, it is working perfectly.
Here you go!
package main
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
"net/http"
"os"
)
func main() {
// DO NOT PUT credentials in code for production usage!
// see https://www.socketloop.com/tutorials/golang-setting-up-configure-aws-credentials-with-official-aws-sdk-go
// on setting creds from environment or loading from file
// the file location and load default profile
creds := credentials.NewSharedCredentials("/<change this>/.aws/credentials", "default")
_, err := creds.Get()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
aws.DefaultConfig.Region = "us-east-1" //<--- change this to yours
config := &aws.Config{
Region: "",
Endpoint: "s3.amazonaws.com", // <-- forking important !
S3ForcePathStyle: true, // <-- without these lines. All will fail! fork you aws!
Credentials: creds,
LogLevel: 0, // <-- feel free to crank it up
}
s3client := s3.New(config)
bucketName := "<change>" // <-- change this to your bucket name
fileToUpload := "<change>"
file, err := os.Open(fileToUpload)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
fileInfo, _ := file.Stat()
var size int64 = fileInfo.Size()
buffer := make([]byte, size)
// read file content to buffer
file.Read(buffer)
fileBytes := bytes.NewReader(buffer) // convert to io.ReadSeeker type
fileType := http.DetectContentType(buffer)
path := "/examplefolder/" + file.Name() // target file and location in S3
params := &s3.PutObjectInput{
Bucket: aws.String(bucketName), // required
Key: aws.String(path), // required
ACL: aws.String("public-read"),
Body: fileBytes,
ContentLength: aws.Long(size),
ContentType: aws.String(fileType),
Metadata: map[string]*string{
"Key": aws.String("MetadataValue"), //required
},
// see more at http://godoc.org/github.com/aws/aws-sdk-go/service/s3#S3.PutObject
}
result, err := s3client.PutObject(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
// Generic AWS Error with Code, Message, and original error (if any)
fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
if reqErr, ok := err.(awserr.RequestFailure); ok {
// A service error occurred
fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
}
} else {
// This case should never be hit, the SDK should always return an
// error which satisfies the awserr.Error interface.
fmt.Println(err.Error())
}
}
fmt.Println(awsutil.StringValue(result))
}
Sample output :
...
Content-Type: application/zip
X-Amz-Acl: public-read
X-Amz-Content-Sha256: b39a762d032dd518aacf51cbee2a70a3bc90e24ceddc6cd3c94d29b0882beaa0
X-Amz-Date: 20150610T084114Z
X-Amz-Meta-Key: MetadataValue
Accept-Encoding: gzip
---[ RESPONSE ]--------------------------------------
HTTP/1.1 200 OK
{
ETag: "\"04f9ed14026972f61407e98b0cbe6445\""
}
References :
https://www.socketloop.com/tutorials/golang-upload-and-download-file-to-from-aws-s3
http://godoc.org/github.com/aws/aws-sdk-go/service/s3#S3.PutObject
See also : Golang : Create S3 bucket with official aws-sdk-go package
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+19.3k Golang : Convert date string to variants of time.Time type examples
+4.6k Golang : Get S3 or CloudFront object or file information
+22.1k Golang : Fix type interface{} has no field or no methods and type assertions example
+19.9k Golang : How to get time zone and load different time zone?
+6.5k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+7.4k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+8.4k Golang : How to protect your source code from client, hosting company or hacker?
+3.9k Golang : Customize scanner.Scanner to treat dash as part of identifier
+22.8k Golang : Find biggest/largest number in array
+37.5k Golang : Convert to io.ReadSeeker type
+12.9k Golang : Get uploaded file name or access uploaded files
+13.6k Golang : Accurate and reliable decimal calculations