func Example_listObjects() { // see the auth example how to initiate a context. ctx := cloud.NewContext("project-id", &http.Client{Transport: nil}) var query *storage.Query for { // If you are using this package on App Engine Managed VMs runtime, // you can init a bucket client with your app's default bucket name. // See http://godoc.org/google.golang.org/appengine/file#DefaultBucketName. objects, err := storage.List(ctx, "bucketname", query) if err != nil { log.Fatal(err) } for _, obj := range objects.Results { log.Printf("object name: %s, size: %v", obj.Name, obj.Size) } // if there are more results, objects.Next // will be non-nil. query = objects.Next if query == nil { break } } log.Println("paginated through all object items in the bucket you specified.") }
// listBucket lists the contents of a bucket in Google Cloud Storage. func (d *demo) listBucket() { io.WriteString(d.w, "\nListbucket result:\n") query := &storage.Query{Prefix: "foo"} for query != nil { objs, err := storage.List(d.ctx, d.bucket, query) if err != nil { d.errorf("listBucket: unable to list bucket %q: %v", d.bucket, err) return } query = objs.Next for _, obj := range objs.Results { d.dumpStats(obj) } } }
func (d *demo) listDir(name, indent string) { query := &storage.Query{Prefix: name, Delimiter: "/"} for query != nil { objs, err := storage.List(d.ctx, d.bucket, query) if err != nil { d.errorf("listBucketDirMode: unable to list bucket %q: %v", d.bucket, err) return } query = objs.Next for _, obj := range objs.Results { fmt.Fprint(d.w, indent) d.dumpStats(obj) } for _, dir := range objs.Prefixes { fmt.Fprintf(d.w, "%v(directory: /%v/%v)\n", indent, d.bucket, dir) d.listDir(dir, indent+" ") } } }