route/vendor/github.com/aws/aws-sdk-go/doc-src/aws-godoc/templates/user_guide_example.html

83 lines
2.9 KiB
HTML

<html>
<head>
<body>
<h2> AWS SDK for Go </h2>
<p>
aws-sdk-go is the official AWS SDK for the Go programming language.
Checkout our release notes for information about the latest bug fixes, updates, and features added to the SDK.
</p>
<h3> Installing </h3>
<p>
If you are using Go 1.5 with the GO15VENDOREXPERIMENT=1 vendoring flag you can use the following to get the SDK as the SDK's runtime dependencies are vendored in the vendor folder.
</p>
<pre class="bash"> $ go get -u github.com/aws/aws-sdk-go </pre>
<p>
Otherwise you'll need to tell Go to get the SDK and all of its dependencies.
</p>
<pre> $ go get -u github.com/aws/aws-sdk-go/... </pre>
<h3> Configuring Credentials </h3>
<p>
Before using the SDK, ensure that you've configured credentials. The best way to configure credentials on a development machine is to use the ~/.aws/credentials file, which might look like:
</p>
<pre>
[default]
aws_access_key_id = AKID1234567890
aws_secret_access_key = MY-SECRET-KEY
</pre>
<p>
You can learn more about the credentials file from this blog post.
Alternatively, you can set the following environment variables:
</p>
<pre>
AWS_ACCESS_KEY_ID=AKID1234567890
AWS_SECRET_ACCESS_KEY=MY-SECRET-KEY
</pre>
<h4> AWS CLI config file (~/aws/config) </h4>
<p>
The AWS SDK for Go does not support the AWS CLI's config file. The SDK will not use any contents from this file. The SDK only supports the shared credentials file (~/aws/credentials). #384 tracks this feature request discussion.
</p>
<h3> Using the Go SDK </h3>
<p>
To use a service in the SDK, create a service variable by calling the New() function. Once you have a service client, you can call API operations which each return response data and a possible error.
To list a set of instance IDs from EC2, you could run:
</p>
<pre>
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/ec2"
)
func main() {
// Create an EC2 service object in the "us-west-2" region
// Note that you can also configure your region globally by
// exporting the AWS_REGION environment variable
svc := ec2.New(session.New(), &aws.Config{Region: aws.String("us-west-2")})
// Call the DescribeInstances Operation
resp, err := svc.DescribeInstances(nil)
if err != nil {
panic(err)
}
// resp has all of the response data, pull out instance IDs:
fmt.Println("> Number of reservation sets: ", len(resp.Reservations))
for idx, res := range resp.Reservations {
fmt.Println(" > Number of instances: ", len(res.Instances))
for _, inst := range resp.Reservations[idx].Instances {
fmt.Println(" - Instance ID: ", *inst.InstanceId)
}
}
}
</pre>
</body>
</head>
</html>