How do I use AWS SDK for Go (Golang) with Wasabi?

AWS SDK for GO has been certified for use with Wasabi. 

 

To use the GO SDK execute the following steps:

1) Install the AWS SDK for GO

2) Configure additional AWS CLI profile for Wasabi account using the Wasabi keys (optional)

In this example, we have set the profile name as "wasabi" in the "~/.aws/credentials" file. To help our customers use this SDK with Wasabi, we have provided examples for both IAM and S3. This example shows: 

  1. How to set the credentials.
  2. Connect to IAM and S3 endpoints
  3. Create a user using IAM
  4. Create a Bucket
  5. Upload an Object to the Bucket
  6. Read an Object from the Bucket
  7. Delete the Object from the Bucket

Other examples can be referred from the AWS documentation.   

Note that this example discusses the use of Wasabi's us-east-1 storage region. To use other Wasabi storage regions, please use the appropriate Wasabi service URL as described in this article

1. How to set the credentials

package main

import (
  "fmt"
  "github.com/aws/aws-sdk-go/aws"
  "github.com/aws/aws-sdk-go/aws/session"
)

func main() {
  //// create a configuration
  //s3Config := aws.Config{
  // Credentials:      credentials.NewStaticCredentials("<access-key>", "<secret-key>", ""),
  // Endpoint:         aws.String("https://s3.wasabisys.com"),
  // Region:           aws.String("us-east-1"),
  // S3ForcePathStyle: aws.Bool(true),
  //}
  //
  //// create a session with the configuration above
  //goSession, err := session.NewSessionWithOptions(session.Options{
  // Config: s3Config,
  //})

  // create a configuration for profile
  s3Config := aws.Config{
    Endpoint:         aws.String("https://s3.wasabisys.com"),
    Region:           aws.String("us-east-1"),
    S3ForcePathStyle: aws.Bool(true),
  }

  // create a new session using the config above and profile
  goSession, err := session.NewSessionWithOptions(session.Options{
    Config:  s3Config,
    Profile: "wasabi",
  })

  // check if the session was created correctly.
  if err != nil {
    fmt.Println(err)
  }
}

2.Connect to IAM and S3 endpoints

IAM:

package main

import (
  "fmt"
  "github.com/aws/aws-sdk-go/aws"
  "github.com/aws/aws-sdk-go/aws/session"
  "github.com/aws/aws-sdk-go/service/iam"
)

func main() {
  //// create a configuration [IAM: the endpoint is different]
  //s3Config := aws.Config{
  // Credentials:      credentials.NewStaticCredentials("<access-key>", "<secret-key>", ""),
  // Endpoint:         aws.String("https://iam.wasabisys.com"),
  // Region:           aws.String("us-east-1"),
  // S3ForcePathStyle: aws.Bool(true),
  //}
  //
  //// create a session with the configuration above
  //go_session, err := session.NewSessionWithOptions(session.Options{
  // Config: s3Config,
  //})

  // create a configuration for profile [IAM: the endpoint is different]
  s3Config := aws.Config{
    Endpoint:         aws.String("https://iam.wasabisys.com"),
    Region:           aws.String("us-east-1"),
    S3ForcePathStyle: aws.Bool(true),
  }

  // create a new session using the config above and profile
  goSession, err := session.NewSessionWithOptions(session.Options{
    Config:  s3Config,
    Profile: "wasabi",
  })

  // check if the session was created correctly.
  if err != nil {
    fmt.Println(err)
  }

  // create an iam client
  iamClient := iam.New(goSession)
}

S3:

package main

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

func main() {
  //// create a configuration
  //s3Config := aws.Config{
  // Credentials:      credentials.NewStaticCredentials("<access-key>", "<secret-key>", ""),
  // Endpoint:         aws.String("https://s3.wasabisys.com"),
  // Region:           aws.String("us-east-1"),
  // S3ForcePathStyle: aws.Bool(true),
  //}
  //
  //// create a session with the configuration above
  //goSession, err := session.NewSessionWithOptions(session.Options{
  // Config: s3Config,
  //})

  // create a configuration for profile
  s3Config := aws.Config{
    Endpoint:         aws.String("https://s3.wasabisys.com"),
    Region:           aws.String("us-east-1"),
    S3ForcePathStyle: aws.Bool(true),
  }

  // create a new session using the config above and profile
  goSession, err := session.NewSessionWithOptions(session.Options{
    Config:  s3Config,
    Profile: "wasabi",
  })

  // check if the session was created correctly.
  if err != nil {
    fmt.Println(err)
  }

  // create a s3 client session
  s3Client := s3.New(goSession)
}

3. Create a user using IAM

package main

import (
  "fmt"
  "github.com/aws/aws-sdk-go/aws"
  "github.com/aws/aws-sdk-go/aws/session"
  "github.com/aws/aws-sdk-go/service/iam"
)

func main() {
  //// create a configuration [IAM: the endpoint is different]
  //s3Config := aws.Config{
  // Credentials:      credentials.NewStaticCredentials("<access-key>", "<secret-key>", ""),
  // Endpoint:         aws.String("https://iam.wasabisys.com"),
  // Region:           aws.String("us-east-1"),
  // S3ForcePathStyle: aws.Bool(true),
  //}
  //
  //// create a session with the configuration above
  //go_session, err := session.NewSessionWithOptions(session.Options{
  // Config: s3Config,
  //})

  // create a configuration for profile [IAM: the endpoint is different]
  s3Config := aws.Config{
    Endpoint:         aws.String("https://iam.wasabisys.com"),
    Region:           aws.String("us-east-1"),
    S3ForcePathStyle: aws.Bool(true),
  }

  // create a new session using the config above and profile
  goSession, err := session.NewSessionWithOptions(session.Options{
    Config:  s3Config,
    Profile: "wasabi",
  })

  // check if the session was created correctly.
  if err != nil {
    fmt.Println(err)
  }

  // create an iam client
  iamClient := iam.New(goSession)

  // Iam input to create user
  iamInput := &iam.CreateUserInput{UserName: aws.String("go-user")}

  // Iam call to create user
  _, err = iamClient.CreateUser(iamInput)

  // output any errors
  if err != nil {
    fmt.Println(err.Error())
  }
}

4. Create a Bucket

package main

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

func main() {
  //// create a configuration
  //s3Config := aws.Config{
  // Credentials:      credentials.NewStaticCredentials("<access-key>", "<secret-key>", ""),
  // Endpoint:         aws.String("https://s3.wasabisys.com"),
  // Region:           aws.String("us-east-1"),
  // S3ForcePathStyle: aws.Bool(true),
  //}
  //
  //// create a session with the configuration above
  //goSession, err := session.NewSessionWithOptions(session.Options{
  // Config: s3Config,
  //})

  // create a configuration for profile
  s3Config := aws.Config{
    Endpoint:         aws.String("https://s3.wasabisys.com"),
    Region:           aws.String("us-east-1"),
    S3ForcePathStyle: aws.Bool(true),
  }

  // create a new session using the config above and profile
  goSession, err := session.NewSessionWithOptions(session.Options{
    Config:  s3Config,
    Profile: "wasabi",
  })

  // check if the session was created correctly.
  if err != nil {
    fmt.Println(err)
  }

  // create a s3 client session
  s3Client := s3.New(goSession)

  // set parameter for bucket name
  bucket := aws.String("<bucket-name>")

  // create a bucket
  _, err = s3Client.CreateBucket(&s3.CreateBucketInput{
    Bucket: bucket,
  })

  // print if there is an error
  if err != nil {
    fmt.Println(err.Error())
    return
  }
}

5. Upload an object to the Bucket

package main

import (
  "fmt"
  "github.com/aws/aws-sdk-go/aws"
  "github.com/aws/aws-sdk-go/aws/session"
  "github.com/aws/aws-sdk-go/service/s3"
  "os"
)

func main() {
  //// create a configuration
  //s3Config := aws.Config{
  // Credentials:      credentials.NewStaticCredentials("<access-key>", "<secret-key>", ""),
  // Endpoint:         aws.String("https://s3.wasabisys.com"),
  // Region:           aws.String("us-east-1"),
  // S3ForcePathStyle: aws.Bool(true),
  //}
  //
  //// create a session with the configuration above
  //go_session, err := session.NewSessionWithOptions(session.Options{
  // Config: s3Config,
  //})

  // create a configuration for profile
  s3Config := aws.Config{
    Endpoint:         aws.String("https://s3.wasabisys.com"),
    Region:           aws.String("us-east-1"),
    S3ForcePathStyle: aws.Bool(true),
  }

  // create a new session using the config above and profile
  goSession, err := session.NewSessionWithOptions(session.Options{
    Config:  s3Config,
    Profile: "wasabi",
  })

  // check if the session was created correctly.
  if err != nil {
    fmt.Println(err)
  }

  // create a s3 client session
  s3Client := s3.New(goSession)

  //set the file path to upload
  file, err := os.Open("<file-path>")
  if err != nil {
    fmt.Println(err.Error())
    return
  }
  defer file.Close()

  // create put object input
  putObjectInput := &s3.PutObjectInput{
    Body:   file,
    Bucket: aws.String("go-bucket-rv"),
    Key:    aws.String("text.txt"),
  }
  // upload file
  _, err = s3Client.PutObject(putObjectInput)

  // print if there is an error
  if err != nil {
    fmt.Println(err.Error())
    return
  }
}

6. Read an object from the Bucket

package main

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

func main() {
  //// create a configuration
  //s3Config := aws.Config{
  // Credentials:      credentials.NewStaticCredentials("<access-key>", "<secret-key>", ""),
  // Endpoint:         aws.String("https://s3.wasabisys.com"),
  // Region:           aws.String("us-east-1"),
  // S3ForcePathStyle: aws.Bool(true),
  //}
  //
  //// create a session with the configuration above
  //go_session, err := session.NewSessionWithOptions(session.Options{
  // Config: s3Config,
  //})

  // create a configuration for profile
  s3Config := aws.Config{
    Endpoint:         aws.String("https://s3.wasabisys.com"),
    Region:           aws.String("us-east-1"),
    S3ForcePathStyle: aws.Bool(true),
  }

  // create a new session using the config above and profile
  goSession, err := session.NewSessionWithOptions(session.Options{
    Config:  s3Config,
    Profile: "wasabi",
  })

  // check if the session was created correctly.
  if err != nil {
    fmt.Println(err)
  }

  // create a s3 client session
  s3Client := s3.New(goSession)

  // create put object input
  getObjectInput := &s3.GetObjectInput{
    Bucket: aws.String("go-bucket-rv"),
    Key:    aws.String("text.txt"),
  }
  // get file
  _, err = s3Client.GetObject(getObjectInput)

  // print if there is an error
  if err != nil {
    fmt.Println(err.Error())
    return
  }
}

7. Delete the object from the Bucket

package main

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

func main() {
  //// create a configuration
  //s3Config := aws.Config{
  // Credentials:      credentials.NewStaticCredentials("<access-key>", "<secret-key>", ""),
  // Endpoint:         aws.String("https://s3.wasabisys.com"),
  // Region:           aws.String("us-east-1"),
  // S3ForcePathStyle: aws.Bool(true),
  //}
  //
  //// create a session with the configuration above
  //go_session, err := session.NewSessionWithOptions(session.Options{
  // Config: s3Config,
  //})

  // create a configuration for profile
  s3Config := aws.Config{
    Endpoint:         aws.String("https://s3.wasabisys.com"),
    Region:           aws.String("us-east-1"),
    S3ForcePathStyle: aws.Bool(true),
  }

  // create a new session using the config above and profile
  goSession, err := session.NewSessionWithOptions(session.Options{
    Config:  s3Config,
    Profile: "wasabi",
  })

  // check if the session was created correctly.
  if err != nil {
    fmt.Println(err)
  }

  // create a s3 client session
  s3Client := s3.New(goSession)

  // create put object input
  deleteObjectInput := &s3.DeleteObjectInput{
    Bucket: aws.String("go-bucket-rv"),
    Key:    aws.String("text.txt"),
  }
  // get file
  _, err = s3Client.DeleteObject(deleteObjectInput)

  // print if there is an error
  if err != nil {
    fmt.Println(err.Error())
    return
  }
}

 

Have more questions? Submit a request