Created
May 19, 2016 07:11
-
-
Save suzuken/54971f422f97f9ac86448addbdec81b4 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 ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"golang.org/x/net/context" | |
"golang.org/x/oauth2/google" | |
bigquery "google.golang.org/api/bigquery/v2" | |
storage "google.golang.org/api/storage/v1" | |
) | |
func BigQueryScope() string { | |
return fmt.Sprintf("%s %s %s", | |
bigquery.BigqueryScope, | |
storage.DevstorageReadOnlyScope, | |
"https://www.googleapis.com/auth/userinfo.profile") | |
} | |
var ( | |
bucket = flag.String("bucket", "kuke", "list buckets name") | |
prefix = flag.String("prefix", "your-object-prefix", "prefix name") | |
credential = flag.String("credential", "/path/to/credential", "path to service account credential JSON file") | |
) | |
func main() { | |
flag.Parse() | |
b, err := ioutil.ReadFile(*credential) | |
if err != nil { | |
log.Fatalf("failed to read %s, %err", *credential, err) | |
} | |
conf, err := google.JWTConfigFromJSON(b, BigQueryScope()) | |
if err != nil { | |
log.Fatalf("fail %s", err) | |
} | |
ctx := context.Background() | |
client := conf.Client(ctx) | |
service, err := storage.New(client) | |
if err != nil { | |
log.Fatalf("Unable to create Storage service: %v", err) | |
} | |
list := service.Objects.List(*bucket) | |
list.Prefix(*prefix) | |
objects, err := list.Do() | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
return | |
} | |
for _, o := range objects.Items { | |
log.Printf("item: %v\n", o) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment