Last active
August 31, 2020 12:42
-
-
Save sonmezonur/b0f93edf42e36022155829daa75f6084 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"net/http" | |
"os" | |
"os/signal" | |
"syscall" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/endpoints" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/s3/s3manager" | |
) | |
func main() { | |
var ( | |
flagEndpoint = flag.String("endpoint", "", "endpoint url") | |
flagBucket = flag.String("bucket", "", "bucket name") | |
) | |
flag.Parse() | |
ctx, cancel := context.WithCancel(context.Background()) | |
defer cancel() | |
go func() { | |
sigch := make(chan os.Signal, 1) | |
signal.Notify(sigch, os.Interrupt, syscall.SIGTERM) | |
<-sigch | |
cancel() | |
signal.Stop(sigch) | |
}() | |
sess, err := newSession(*flagEndpoint) | |
if err != nil { | |
fmt.Printf("could not create a session: %v", err) | |
return | |
} | |
region, err := s3manager.GetBucketRegion(ctx, sess, *flagBucket, "") | |
if err != nil { | |
fmt.Printf("could not get region location: %v", err) | |
return | |
} | |
fmt.Printf("region: %v\n", region) | |
} | |
func newSession(endpoint string) (*session.Session, error) { | |
awsCfg := aws.NewConfig() | |
var httpClient *http.Client | |
awsCfg = awsCfg. | |
WithRegion(endpoints.UsEast1RegionID). // use some default region | |
WithEndpoint(endpoint). | |
WithHTTPClient(httpClient) | |
return session.NewSessionWithOptions( | |
session.Options{ | |
Config: *awsCfg, | |
}, | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment