mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-30 04:20:53 +00:00
Upgrade AWS SDK to the latest version
This commit is contained in:
+49
@@ -0,0 +1,49 @@
|
||||
Retrieve Credentials with Go Plugin
|
||||
===
|
||||
|
||||
This example demonstrates how you can take advantage of Go 1.8's new Plugin
|
||||
functionality to retrieve AWS credentials dynamically from a plugin compiled
|
||||
separate from your application.
|
||||
|
||||
Usage
|
||||
---
|
||||
|
||||
Example Plugin
|
||||
---
|
||||
|
||||
You can find the plugin at `plugin/plugin.go` nested within this example. The plugin
|
||||
demonstrates what symbol the SDK will use when lookup up the credential provider
|
||||
and the type signature that needs to be implemented.
|
||||
|
||||
Compile the plugin with:
|
||||
|
||||
go build -tags example -o myPlugin.so -buildmode=plugin plugin/plugin.go
|
||||
|
||||
JSON Credentials File
|
||||
---
|
||||
|
||||
This example plugin will read the credentials from a JSON file pointed to by
|
||||
the `PLUGIN_CREDS_FILE` environment variable. The contents of the file are
|
||||
the credentials, Key, Secret, and Token. The `Token` filed does not need to be
|
||||
set if your credentials do not have one.
|
||||
|
||||
```json
|
||||
{
|
||||
"Key": "MyAWSCredAccessKeyID",
|
||||
"Secret": "MyAWSCredSecretKey",
|
||||
"Token": "MyAWSCredToken"
|
||||
}
|
||||
```
|
||||
|
||||
Example Application
|
||||
---
|
||||
|
||||
The `main.go` file in this folder demonstrates how you can configure the SDK to
|
||||
use a plugin to retrieve credentials with.
|
||||
|
||||
Compile and run application:
|
||||
|
||||
go build -tags example -o myApp main.go
|
||||
|
||||
PLUGIN_CREDS_FILE=pathToCreds.json ./myApp myPlugin.so myBucket myObjectKey
|
||||
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
// +build example,go18
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"plugin"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials/plugincreds"
|
||||
"github.com/aws/aws-sdk-go/aws/endpoints"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||
)
|
||||
|
||||
// Example application which loads a Go Plugin file, and uses the credential
|
||||
// provider defined within the plugin to get credentials for making a S3
|
||||
// request.
|
||||
//
|
||||
// The example will derive the bucket's region automatically if a AWS_REGION
|
||||
// environment variable is not defined.
|
||||
//
|
||||
// Build:
|
||||
// go build -tags example -o myApp main.go
|
||||
//
|
||||
// Usage:
|
||||
// ./myApp <compiled plugin> <bucket> <object key>
|
||||
func main() {
|
||||
if len(os.Args) < 4 {
|
||||
exitErrorf("Usage: myApp <compiled plugin>, <bucket> <object key>")
|
||||
}
|
||||
|
||||
pluginFilename := os.Args[1]
|
||||
bucket := os.Args[2]
|
||||
key := os.Args[3]
|
||||
|
||||
// Open plugin, and load it into the process.
|
||||
p, err := plugin.Open(pluginFilename)
|
||||
if err != nil {
|
||||
exitErrorf("failed to open plugin, %s, %v", pluginFilename, err)
|
||||
}
|
||||
|
||||
// Create a new Credentials value which will source the provider's Retrieve
|
||||
// and IsExpired functions from the plugin.
|
||||
creds, err := plugincreds.NewCredentials(p)
|
||||
if err != nil {
|
||||
exitErrorf("failed to load plugin provider, %v", err)
|
||||
}
|
||||
|
||||
// Example to configure a Session with the newly created credentials that
|
||||
// will be sourced using the plugin's functionality.
|
||||
sess := session.Must(session.NewSession(&aws.Config{
|
||||
Credentials: creds,
|
||||
}))
|
||||
|
||||
// If the region is not available attempt to derive the bucket's region
|
||||
// from a query to S3 for the bucket's metadata
|
||||
region := aws.StringValue(sess.Config.Region)
|
||||
if len(region) == 0 {
|
||||
region, err = s3manager.GetBucketRegion(context.Background(), sess, bucket, endpoints.UsEast1RegionID)
|
||||
if err != nil {
|
||||
exitErrorf("failed to get bucket region, %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Create the S3 service client for the target region
|
||||
svc := s3.New(sess, aws.NewConfig().WithRegion(region))
|
||||
|
||||
// Get the object's details
|
||||
result, err := svc.HeadObject(&s3.HeadObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
fmt.Println(result, err)
|
||||
}
|
||||
|
||||
func exitErrorf(format string, args ...interface{}) {
|
||||
fmt.Fprintf(os.Stderr, format+"\n", args...)
|
||||
os.Exit(1)
|
||||
}
|
||||
Generated
Vendored
+61
@@ -0,0 +1,61 @@
|
||||
// +build example,go18
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Example plugin that will retrieve credentials from a JSON file that the
|
||||
// "PLUGIN_CREDS_FILE" environment variable points to
|
||||
//
|
||||
// Build with:
|
||||
// go build -tags example -o plugin.so -buildmode=plugin plugin.go
|
||||
func main() {}
|
||||
|
||||
var myCredProvider provider
|
||||
|
||||
func init() {
|
||||
// Initialize a mock credential provider with stubs
|
||||
myCredProvider = provider{Filename: os.Getenv("PLUGIN_CREDS_FILE")}
|
||||
}
|
||||
|
||||
// GetAWSSDKCredentialProvider is the symbol SDK will lookup and use to
|
||||
// get the credential provider's retrieve and isExpired functions.
|
||||
func GetAWSSDKCredentialProvider() (func() (key, secret, token string, err error), func() bool) {
|
||||
return myCredProvider.Retrieve, myCredProvider.IsExpired
|
||||
}
|
||||
|
||||
// mock implementation of a type that returns retrieves credentials and
|
||||
// returns if they have expired.
|
||||
type provider struct {
|
||||
Filename string
|
||||
|
||||
loaded bool
|
||||
}
|
||||
|
||||
func (p *provider) Retrieve() (key, secret, token string, err error) {
|
||||
f, err := os.Open(p.Filename)
|
||||
if err != nil {
|
||||
return "", "", "", errors.Wrapf(err, "failed to open credentials file, %q", p.Filename)
|
||||
}
|
||||
decoder := json.NewDecoder(f)
|
||||
|
||||
creds := struct {
|
||||
Key, Secret, Token string
|
||||
}{}
|
||||
|
||||
if err := decoder.Decode(&creds); err != nil {
|
||||
return "", "", "", errors.Wrap(err, "failed to decode credentials file")
|
||||
}
|
||||
|
||||
p.loaded = true
|
||||
return creds.Key, creds.Secret, creds.Token, nil
|
||||
}
|
||||
|
||||
func (p *provider) IsExpired() bool {
|
||||
return !p.loaded
|
||||
}
|
||||
+9
-1
@@ -3,4 +3,12 @@ Custom Endpoint Example
|
||||
|
||||
This example provides examples on how you can provide custom endpoints, and logic to how endpoints are resolved by the SDK.
|
||||
|
||||
The example creates multiple clients with different endpoint configuraiton. From a custom endpoint resolver that wraps the defeault resolver so that any S3 service client created uses the custom endpoint, to how you can provide your own logic to a single service's endpoint resolving.
|
||||
The example creates multiple clients with different endpoint configuration. From a custom endpoint resolver that wraps the default resolver so that any Amazon S3 service client created uses the custom endpoint, to how you can provide your own logic to a single service's endpoint resolving.
|
||||
|
||||
|
||||
Usage
|
||||
---
|
||||
|
||||
```sh
|
||||
go run -tags example customeEndpoint.go
|
||||
```
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
Enumerate Regions and Endpoints Example
|
||||
===
|
||||
|
||||
Demostrates how the SDK's endpoints can be enumerated over to discover regions, services, and endpoints defined by the SDK's Regions and Endpoints metadata.
|
||||
Demonstrates how the SDK's endpoints can be enumerated over to discover regions, services, and endpoints defined by the SDK's Regions and Endpoints metadata.
|
||||
|
||||
Usage
|
||||
---
|
||||
@@ -10,7 +10,7 @@ The following parameters can be used to enumerate the SDK's partition metadata.
|
||||
|
||||
Example:
|
||||
|
||||
go run enumEndpoints.go -p aws -services -r us-west-2
|
||||
go run -tags example enumEndpoints.go -p aws -services -r us-west-2
|
||||
|
||||
Output:
|
||||
|
||||
|
||||
+1
-1
@@ -10,4 +10,4 @@ Will default to shared config file, but can load from environment if provided.
|
||||
## Usage:
|
||||
|
||||
# Upload myfile.txt to myBucket/myKey. Must complete within 10 minutes or will fail
|
||||
go run withContext.go -b mybucket -k myKey -d 10m < myfile.txt
|
||||
go run -tags example withContext.go -b mybucket -k myKey -d 10m < myfile.txt
|
||||
|
||||
+21
-6
@@ -10,6 +10,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
"github.com/aws/aws-sdk-go/aws/request"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
)
|
||||
@@ -36,21 +38,34 @@ func main() {
|
||||
sess := session.Must(session.NewSession())
|
||||
svc := s3.New(sess)
|
||||
|
||||
// Create a context with a timeout that will abort the upload if it takes
|
||||
// more than the passed in timeout.
|
||||
ctx := context.Background()
|
||||
var cancelFn func()
|
||||
if timeout > 0 {
|
||||
ctx, cancelFn = context.WithTimeout(ctx, timeout)
|
||||
}
|
||||
// Ensure the context is canceled to prevent leaking.
|
||||
// See context package for more information, https://golang.org/pkg/context/
|
||||
defer cancelFn()
|
||||
|
||||
// Uploads the object to S3. The Context will interrupt the request
|
||||
resp, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
|
||||
// Uploads the object to S3. The Context will interrupt the request if the
|
||||
// timeout expires.
|
||||
_, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
Body: os.Stdin,
|
||||
})
|
||||
if err != nil {
|
||||
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == request.CanceledErrorCode {
|
||||
// If the SDK can determine the request or retry delay was canceled
|
||||
// by a context the CanceledErrorCode error code will be returned.
|
||||
fmt.Fprintf(os.Stderr, "upload canceled due to timeout, %v\n", err)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "failed to upload object, %v\n", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(resp, err)
|
||||
|
||||
// Cleanup context
|
||||
cancelFn()
|
||||
fmt.Printf("successfully uploaded file to %s/%s\n", bucket, key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user