1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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)
}
|