- 13 Aug 2024
- 4 Minutes to read
- Print
- PDF
AWS SDK for Go (Golang) With Wasabi
- Updated on 13 Aug 2024
- 4 Minutes to read
- Print
- PDF
How do I use AWS SDK for Go (Golang) with Wasabi?
AWS SDK for GO has been certified for use with Wasabi.
Using the GO SDK
Execute the following steps:
Install the AWS SDK for GO.
(Optional) Configure an additional AWS CLI profile for Wasabi account using the Wasabi keys.
In this example, we set the profile name as "wasabi" in the "~/.aws/credentials" file. To help you use this SDK with Wasabi, we provided examples for both IAM and S3:
Connecting to IAM and S3 Endpoints
Creating a User Using IAM
Creating a Bucket
Uploading an Object to the Bucket
Reading an Object From the Bucket
Deleting an Object From the Bucket
Additional examples are available from the AWS documentation.
The examples use Wasabi's us-east-1 storage region. For other Wasabi storage regions, use the appropriate Wasabi service URL.
Send all IAM requests to iam.wasabisys.com.
Connecting to IAM and S3 Endpoints
IAM
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/iam"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-east-1"),
config.WithEndpointResolverWithOptions(
aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == iam.ServiceID {
return aws.Endpoint{
URL: "https://iam.wasabisys.com",
SigningRegion: "us-east-1",
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
},
),
),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
iamClient := iam.NewFromConfig(cfg)
_, err = iamClient.ListUsers(context.TODO(), &iam.ListUsersInput{})
if err != nil {
log.Fatalf("unable to list IAM users, %v", err)
}
fmt.Println("Successfully connected to IAM service")
} }
S3
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-east-1"),
config.WithEndpointResolverWithOptions(
aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID {
return aws.Endpoint{
URL: "https://s3.wasabisys.com",
SigningRegion: "us-east-1",
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
},
),
),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
s3Client := s3.NewFromConfig(cfg)
_, err = s3Client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
if err != nil {
log.Fatalf("unable to list S3 buckets, %v", err)
}
fmt.Println("Successfully connected to S3 service")
}
Creating a User Using IAM
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/iam"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-east-1"),
config.WithEndpointResolverWithOptions(
aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == iam.ServiceID {
return aws.Endpoint{
URL: "https://iam.wasabisys.com",
SigningRegion: "us-east-1",
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
},
),
),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
iamClient := iam.NewFromConfig(cfg)
userName := "iam-user-name"
createUserOutput, err := iamClient.CreateUser(context.TODO(), &iam.CreateUserInput{
UserName: &userName,
})
if err != nil {
log.Fatalf("Unable to create IAM user: %v", err)
}
fmt.Printf("Created user: %s\n", aws.ToString(createUserOutput.User.UserName))
}
Creating a Bucket
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3" )
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-east-1"),
config.WithEndpointResolverWithOptions(
aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID {
return aws.Endpoint{
URL: "https://s3.wasabisys.com",
SigningRegion: "us-east-1",
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
},
),
),
)
if err != nil {
log.Fatalf("Unable to load SDK config, %v", err)
}
s3Client := s3.NewFromConfig(cfg)
bucketName := "bucket-name"
_, err = s3Client.CreateBucket(context.TODO(), &s3.CreateBucketInput{
Bucket: &bucketName,
})
if err != nil {
log.Fatalf("Unable to create bucket: %v", err)
}
fmt.Printf("Bucket %s created successfully\n", bucketName)
}
Uploading an Object to the Bucket
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3" )
func main() {
accessKeyID := "id"
secretAccessKey := "key"
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyID, secretAccessKey, "")),
config.WithRegion("us-east-1"),
config.WithEndpointResolverWithOptions(
aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID {
return aws.Endpoint{
URL: "https://s3.wasabisys.com",
SigningRegion: "us-east-1",
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
},
),
),
)
if err != nil {
fmt.Println("Configuration error:", err)
return
}
client := s3.NewFromConfig(cfg)
filename := "file name with path"
file, err := os.Open(filename)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
uploader := manager.NewUploader(client)
result, err := uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String("bucket-name"),
Key: aws.String(filepath.Base(filename)),
Body: file,
})
if err != nil {
fmt.Println("Error uploading file:", err)
return
}
fmt.Printf("File uploaded successfully: %s\n", result.Location)
}
Reading an Object From the Bucket
package main
import (
"context"
"fmt"
"io/ioutil" "log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-east-1"),
config.WithEndpointResolverWithOptions(
aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID {
return aws.Endpoint{
URL: "https://s3.wasabisys.com",
SigningRegion: "us-east-1",
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
},
),
),
)
if err != nil {
log.Fatalf("Unable to load SDK config, %v", err)
}
s3Client := s3.NewFromConfig(cfg)
bucketName := "bucket-name"
objectKey := "object name with prefix"
getObjectOutput, err := s3Client.GetObject(context.TODO(), &s3.GetObjectInput{
Bucket: &bucketName,
Key: &objectKey,
})
if err != nil {
log.Fatalf("Unable to get object: %v", err)
}
body, err := ioutil.ReadAll(getObjectOutput.Body)
if err != nil {
log.Fatalf("Unable to read object body: %v", err)
}
defer getObjectOutput.Body.Close()
fmt.Printf("Object content: %s\n", string(body))
}
Deleting an Object From the Bucket
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3" )
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-east-1"),
config.WithEndpointResolverWithOptions(
aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID {
return aws.Endpoint{
URL: "https://s3.wasabisys.com",
SigningRegion: "us-east-1",
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
},
),
),
)
if err != nil { log.Fatalf("Unable to load SDK config, %v", err)
}
s3Client := s3.NewFromConfig(cfg)
bucketName := "bucket-name"
objectKey := "Object name with prefix"
_, err = s3Client.DeleteObject(context.TODO(), &s3.DeleteObjectInput{
Bucket: &bucketName,
Key: &objectKey,
})
if err != nil {
log.Fatalf("Unable to delete object: %v", err)
}
fmt.Printf("Object '%s' deleted successfully from bucket '%s'\n", objectKey, bucketName)
}