AWS SDK for Go
The AWS SDK for Go
Getting started
- Create a service account.
- Assign the service account the roles required for your project. For more information about roles, see the Identity and Access Management documentation.
- Create a static access key.
Note
A service account is only allowed to view a list of buckets in the folder it was created in.
A service account can perform actions with objects in buckets that are created in folders different from the service account folder. To enable this, assign the service account roles for the appropriate folder or its bucket.
Installation
To install the AWS SDK for Go, follow the instructions
Setup
-
Go to the
~/.aws/
directory (for macOS and Linux) orC:\Users\<username>\.aws\
(for Windows). -
Create a file named
credentials
with the credentials for Object Storage and copy the following information to it:[default] aws_access_key_id = <static_key_ID> aws_secret_access_key = <secret_key>
-
Create a file named
config
with the default region settings and copy the following information to it:[default] region=ru-central1 endpoint_url=https://storage.yandexcloud.net
Note
Some apps designed to work with Amazon S3 do not allow you to specify the region; this is why Object Storage may also accept the
us-east-1
value.
To access Object Storage, use the https://storage.yandexcloud.net
endpoint.
Code snippets
Development of AWS SDK v.1
Getting a list of bucket names
package main
import (
"context"
"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() {
// Loading configuration from ~/.aws/*
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatal(err)
}
// Creating a client to access S3 storage
client := s3.NewFromConfig(cfg)
// Requesting a list of buckets
result, err := client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
if err != nil {
log.Fatal(err)
}
for _, bucket := range result.Buckets {
log.Printf("bucket=%s creation time=%s", aws.ToString(bucket.Name), bucket.CreationDate.Local().Format("2006-01-02 15:04:05 Monday"))
}
}
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() {
// Creating a custom endpoint listener that will return the correct URL for the S3 service and the ru-central1 region
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID && region == "ru-central1" {
return aws.Endpoint{
PartitionID: "yc",
URL: "https://storage.yandexcloud.net",
SigningRegion: "ru-central1",
}, nil
}
return aws.Endpoint{}, fmt.Errorf("unknown endpoint requested")
})
// Loading configuration from ~/.aws/*
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolverWithOptions(customResolver))
if err != nil {
log.Fatal(err)
}
// Creating a client to access S3 storage
client := s3.NewFromConfig(cfg)
// Requesting a list of buckets
result, err := client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
if err != nil {
log.Fatal(err)
}
for _, bucket := range result.Buckets {
log.Printf("bucket=%s creation time=%s", aws.ToString(bucket.Name), bucket.CreationDate.Format("2006-01-02 15:04:05 Monday"))
}
}
Getting a list of bucket objects
To get a list of objects in the bucket, provide its name in the -b
command line parameter.
package main
import (
"context"
"flag"
"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() {
// Getting the name of the bucket from the command line argument
bucketName := flag.String("b", "", "The name of the bucket")
flag.Parse()
if *bucketName == "" {
fmt.Println("You must supply the name of a bucket (-b BUCKET)")
return
}
// Loading configuration from ~/.aws/*
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatal(err)
}
// Creating a client to access S3 storage
client := s3.NewFromConfig(cfg)
// Requesting a list of all files in the bucket
result, err := client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(*bucketName),
})
if err != nil {
log.Fatal(err)
}
for _, object := range result.Contents {
log.Printf("object=%s size=%d Bytes last modified=%s", aws.ToString(object.Key), aws.ToInt64(object.Size), object.LastModified.Local().Format("2006-01-02 15:04:05 Monday"))
}
}
package main
import (
"context"
"fmt"
"log"
"flag"
"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() {
// Getting the name of the bucket from the command line argument
bucketName := flag.String("b", "", "The name of the bucket")
flag.Parse()
if *bucketName == "" {
fmt.Println("You must supply the name of a bucket (-b BUCKET)")
return
}
// Creating a custom endpoint listener that will return the correct URL for the S3 service and the ru-central1 region
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID && region == "ru-central1" {
return aws.Endpoint{
PartitionID: "yc",
URL: "https://storage.yandexcloud.net",
SigningRegion: "ru-central1",
}, nil
}
return aws.Endpoint{}, fmt.Errorf("unknown endpoint requested")
})
// Loading configuration from ~/.aws/*
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolverWithOptions(customResolver))
if err != nil {
log.Fatal(err)
}
// Creating a client to access S3 storage
client := s3.NewFromConfig(cfg)
// Requesting a list of all files in the bucket
result, err := client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(*bucketName),
})
if err != nil {
log.Fatal(err)
}
for _, object := range result.Contents {
log.Printf("object=%s size=%d Bytes last modified=%s", aws.ToString(object.Key), object.Size, object.LastModified.Format("2006-01-02 15:04:05 Monday"))
}
}
See also the code samples