AWS SDK for Go
The AWS SDK for Go
Getting started
- Create a service account.
- Add the service account to a group to grant it necessary permissions.
- Create a static access key.
Installing
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 authentication data 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 parameters and copy the following information to it:[default] region=eu-north1
Note
Some apps designed to work with Amazon S3 don't let you set the region, so Object Storage also accepts the value
us-east-1
.
To access Object Storage, use the https://storage.ai.nebius.cloud
endpoint.
Code samples
List of bucket names:
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 resolver for returning correct URL for S3 storage in the eu-north1 region
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID && region == "eu-north1" {
return aws.Endpoint{
PartitionID: "yc",
URL: "https://storage.ai.nebius.cloud",
SigningRegion: "eu-north1",
}, 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 the S3 client
client := s3.NewFromConfig(cfg)
// Getting the 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 objects in a bucket:
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 bucket name 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 resolver for returning the correct URL for S3 storage in the eu-north1 region
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID && region == "eu-north1" {
return aws.Endpoint{
PartitionID: "yc",
URL: "https://storage.ai.nebius.cloud",
SigningRegion: "eu-north1",
}, 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 an S3 client
client := s3.NewFromConfig(cfg)
// Getting the 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