Created
March 1, 2016 03:37
-
-
Save jamesandariese/4cae093894090802c15e to your computer and use it in GitHub Desktop.
List Buckets with Regions
This file contains hidden or 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 "fmt" | |
import "log" | |
import "encoding/json" | |
import "github.com/aws/aws-sdk-go/aws/session" | |
import "github.com/aws/aws-sdk-go/service/s3" | |
import "sync" | |
var print_lock sync.Mutex | |
func get_buckets(wg *sync.WaitGroup) { | |
defer wg.Done() | |
svc := s3.New(session.New()) | |
lb_input := &s3.ListBucketsInput{} | |
resp, err := svc.ListBuckets(lb_input) | |
if err != nil { | |
log.Fatalln("Couldn't list buckets", err) | |
} | |
for _, bucket := range resp.Buckets { | |
wg.Add(1) | |
bucket_name := *bucket.Name | |
go func() { | |
defer wg.Done() | |
svc := s3.New(session.New()) | |
gbr_input := &s3.GetBucketLocationInput{ | |
Bucket: &bucket_name, | |
} | |
resp, err := svc.GetBucketLocation(gbr_input) | |
if err != nil { | |
log.Fatalln("Couldn't get bucket location:", bucket, err) | |
} | |
location := "us-east-1" | |
if resp.LocationConstraint != nil { | |
location = *resp.LocationConstraint | |
} | |
jso, err := json.Marshal(map[string]string{ | |
"bucket": bucket_name, | |
"location": location, | |
}) | |
if err != nil { | |
log.Fatalln("Couldn't build JSON output:", bucket_name, location) | |
} | |
func() { | |
print_lock.Lock() | |
defer print_lock.Unlock() | |
fmt.Println(string(jso)) | |
}() | |
}() | |
} | |
} | |
func main() { | |
wg := &sync.WaitGroup{} | |
wg.Add(1) | |
go get_buckets(wg) | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment