AWS Golang SDK

Fundamentals of the AWS SDK for Golang

In order to use the power of the AWS Go SDK, we have to understand two concepts:
Sessions and Service Clients



Sessions

The first is the idea of a session. A session is an object from the SDK that manages configuration we use with other objects to communicate with AWS Services. The session objects can be shared by different aspects of your application code. The session object should be cached and reused. Creating a new session object involves loading the configuration information. Reusing the session data saves resources. Session objects are safe to use concurrently if you don't modify them.

How to create a default session object:

session, err := session.NewSession() 

The information stored in the "session" variable will store default configuration. To overwrite a configuration, pass a pointer to an object of the &aws.Config struct as an argument to NewSession().

session, err := session.NewSession(&aws.Config{
    Region: aws.String("us-east-1"),
})

A second constructor that lets you overwrite session options is NewSessionWithOptions().

session, err := session.NewSessionWithOptions( session.Options{
  Profile: "s3-fullAccess",
})



Service Clients

A service client is an object that provides API access to specific AWS Services such as EC2, S3 or SQS.

Clone Project on github.com/ardeshir/gosdk

package main

import(
    "fmt"
    "log"
    "os"
    u "github.com/ardeshir/version"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/service/s3"
    "github.com/aws/aws-sdk-go/aws/session"
)


var ( 
 debug bool = false
 version string = "0.0.0"
 )


func main() {

sess, err := session.NewSession( &aws.Config{
	Region: aws.String("us-east-1"),
})

if err != nil  {
	log.Fatal(err)
}

s3Svc := s3.New(sess)
results, err := s3Svc.ListBuckets(nil)
if err != nil {
	log.Fatal("Unable to get bucket list")
}

fmt.Println("Buckets: ")
for _, b := range results.Buckets {
	fmt.Printf("Bucket: %s \n", aws.StringValue(b.Name))
}

  if debugTrue() {
    u.V(version)
  }

}

// Function to check env variable DEFAULT_DEBUG bool
func debugTrue() bool {
    
     if os.Getenv("DEFAULT_DEBUG") != "" {
        return true
     }  
     return false 
}