github.com/aavshr/aws-sdk-go@v1.41.3/example/aws/credentials/plugincreds/main.go (about)

     1  //go:build example && go18
     2  // +build example,go18
     3  
     4  package main
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"os"
    10  	"plugin"
    11  
    12  	"github.com/aavshr/aws-sdk-go/aws"
    13  	"github.com/aavshr/aws-sdk-go/aws/credentials/plugincreds"
    14  	"github.com/aavshr/aws-sdk-go/aws/endpoints"
    15  	"github.com/aavshr/aws-sdk-go/aws/session"
    16  	"github.com/aavshr/aws-sdk-go/service/s3"
    17  	"github.com/aavshr/aws-sdk-go/service/s3/s3manager"
    18  )
    19  
    20  // Example application which loads a Go Plugin file, and uses the credential
    21  // provider defined within the plugin to get credentials for making a S3
    22  // request.
    23  //
    24  // The example will derive the bucket's region automatically if a AWS_REGION
    25  // environment variable is not defined.
    26  //
    27  // Build:
    28  //   go build -tags example -o myApp main.go
    29  //
    30  // Usage:
    31  //   ./myApp <compiled plugin> <bucket> <object key>
    32  func main() {
    33  	if len(os.Args) < 4 {
    34  		exitErrorf("Usage: myApp <compiled plugin>, <bucket> <object key>")
    35  	}
    36  
    37  	pluginFilename := os.Args[1]
    38  	bucket := os.Args[2]
    39  	key := os.Args[3]
    40  
    41  	// Open plugin, and load it into the process.
    42  	p, err := plugin.Open(pluginFilename)
    43  	if err != nil {
    44  		exitErrorf("failed to open plugin, %s, %v", pluginFilename, err)
    45  	}
    46  
    47  	// Create a new Credentials value which will source the provider's Retrieve
    48  	// and IsExpired functions from the plugin.
    49  	creds, err := plugincreds.NewCredentials(p)
    50  	if err != nil {
    51  		exitErrorf("failed to load plugin provider, %v", err)
    52  	}
    53  
    54  	// Example to configure a Session with the newly created credentials that
    55  	// will be sourced using the plugin's functionality.
    56  	sess := session.Must(session.NewSession(&aws.Config{
    57  		Credentials: creds,
    58  	}))
    59  
    60  	// If the region is not available attempt to derive the bucket's region
    61  	// from a query to S3 for the bucket's metadata
    62  	region := aws.StringValue(sess.Config.Region)
    63  	if len(region) == 0 {
    64  		region, err = s3manager.GetBucketRegion(context.Background(), sess, bucket, endpoints.UsEast1RegionID)
    65  		if err != nil {
    66  			exitErrorf("failed to get bucket region, %v", err)
    67  		}
    68  	}
    69  
    70  	// Create the S3 service client for the target region
    71  	svc := s3.New(sess, aws.NewConfig().WithRegion(region))
    72  
    73  	// Get the object's details
    74  	result, err := svc.HeadObject(&s3.HeadObjectInput{
    75  		Bucket: aws.String(bucket),
    76  		Key:    aws.String(key),
    77  	})
    78  	fmt.Println(result, err)
    79  }
    80  
    81  func exitErrorf(format string, args ...interface{}) {
    82  	fmt.Fprintf(os.Stderr, format+"\n", args...)
    83  	os.Exit(1)
    84  }