Golang : Create S3 bucket with official aws-sdk-go package
This is just one of the updates/supplementary tutorials to the previous tutorial on how to upload to AWS S3. This tutorial will use the official AWS-SDK-GO package to create a bucket in AWS S3.
NOTE : As of the time of writing this, the AWS-SDK-GO package is still undergoing radical changes and if the code below somehow refused to work properly. Chances are AWS-SDK-GO packages lose internal organs here and there during the metamorphosis period.
Here you go :
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
"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 credentials from environment or loading from file
// this is just for demo/tutorial purpose
aws_access_key_id := "<replace with yours>"
aws_secret_access_key := "<replace with yours>"
token := ""
creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, token)
_, err := creds.Get()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
aws.DefaultConfig.Region = "us-west-2"
s3client := s3.New(&aws.Config{
Region: "us-west-2", // S3 suppose to be global and regionless.. no harm putting this here
Credentials: creds,
LogLevel: 0,
})
// see http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
// for bucket naming rules if you get the error message
// InvalidBucketName The specified bucket is not valid. <nil>
//bucketName := "mynewBucket" <--- will NOT work
bucketName := "ttestmynewbucket"
params := &s3.CreateBucketInput{
Bucket: aws.String(bucketName),
// commented out these as they will cause error. Bug ?
//GrantFullControl: aws.String("GrantFullControl"),
//GrantRead: aws.String("GrantRead"),
//GrantReadACP: aws.String("GrantReadACP"),
//GrantWrite: aws.String("GrantWrite"),
//GrantWriteACP: aws.String("GrantWriteACP"),
}
result, err := s3client.CreateBucket(params)
//if err != nil {
// fmt.Println(err)
// return
// }
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 alwsy return an
// error which satisfies the awserr.Error interface.
fmt.Println(err.Error())
}
}
fmt.Println("Bucket created in ", *result.Location)
}
Output :
Bucket created in http://ttestmynewbucket.s3.amazonaws.com/
References :
https://www.socketloop.com/tutorials/golang-upload-and-download-file-to-from-aws-s3
See also : Golang : Setting up/configure AWS credentials with official aws-sdk-go
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
+4.9k Python : Create Whois client or function example
+5.2k Gogland : Datasource explorer
+16.1k Golang : Execute terminal command to remote machine example
+6k Golang : Scan forex opportunities by Bollinger bands
+12.4k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+17.1k Golang : Check if IP address is version 4 or 6
+19.6k Golang : How to run your code only once with sync.Once object
+7.4k Golang : Convert(cast) io.Reader type to string
+11.8k Golang : Get remaining text such as id or filename after last segment in URL path
+6.5k Golang : Reverse by word
+24.2k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?