Get started with Amazon SDK for Go
The Amazon SDK for Go requires Go 1.20 or later. You can view your current version of Go by running the following command:
go version
For information about installing or upgrading your version of Go,
see
https://golang.org/doc/install
Get an Amazon Account
Before you can use the Amazon SDK for Go v2, you must have an Amazon account. See
How
do I create and activate a new Amazon account?
Install the Amazon SDK for Go v2
The Amazon SDK for Go v2 uses Go modules, which was a feature introduced in Go 1.11. Initialize your local project by running the following Go command.
go mod init example
After initializing your Go module project, you will be able to retrieve the SDK and its required dependencies using the
go get
command. These dependencies will be recorded in the go.mod
file which was created
by the previous command.
The following commands show how to retrieve the standard set of SDK modules to use in your application.
go get github.com/aws/aws-sdk-go-v2 go get github.com/aws/aws-sdk-go-v2/config
This will retrieve the core SDK module, and the config module which is used for loading the Amazon shared configuration.
Next you can install one or more Amazon service API clients required
by your application. All API clients are located under
github.com/aws/aws-sdk-go-v2/service
import
hierarchy. A complete set of currently supported API clients can
be found
herego.mod
file. In this example we retrieve the
Amazon S3 API client.
go get github.com/aws/aws-sdk-go-v2/service/s3
Get your Amazon access keys
Access keys consist of an access key ID and secret access key,
which are used to sign programmatic requests that you make to Amazon.
If you don’t have access keys, you can create them by using the
Amazon
Management Console
Note
To create access keys, you must have permissions to perform the required IAM actions. For more information, see
Granting
IAM User Permission to Manage Password Policy and Credentials
To get your access key ID and secret access key.
-
Open the IAM console
-
On the navigation menu, choose Users.
-
Choose your IAM user name (not the check box).
-
Open the Security credentials tab, and then choose Create access key.
-
To see the new access key, choose Show. Your credentials resemble the following:
-
Access key ID:
AKIAIOSFODNN7EXAMPLE
-
Secret access key:
wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
-
-
To download the key pair, choose Download .csv file. Store the keys in a secure location.
Warning
Keep the keys confidential to protect your Amazon account, and never share them with anyone outside your organization.
Related topics
-
What Is IAM?
in IAM User Guide. -
Amazon Security Credentials
in Amazon Web Services General Reference.
Invoke an Operation
After you have installed the SDK, you import Amazon packages into your Go applications to use the SDK, as shown in the following example, which imports the Amazon, Config, and Amazon S3 libraries. After importing the SDK packages, the Amazon SDK Shared Configuration is loaded, a client is constructed, and an API operation is invoked.
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() { // Load the Shared Amazon Configuration (~/.aws/config) cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { log.Fatal(err) } // Create an Amazon S3 service client client := s3.NewFromConfig(cfg) // Get the first page of results for ListObjectsV2 for a bucket output, err := client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{ Bucket: aws.String("
amzn-s3-demo-bucket
"), }) if err != nil { log.Fatal(err) } log.Println("first page results") for _, object := range output.Contents { log.Printf("key=%s size=%d", aws.ToString(object.Key), *object.Size) } }