MCPcopy Create free account
hub / github.com/google/go-cloud / ExampleBucket_ListPage

Function ExampleBucket_ListPage

blob/example_test.go:321–393  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

319}
320
321func ExampleBucket_ListPage() {
322 // Connect to a bucket when your program starts up.
323 // This example uses the file-based implementation.
324 dir, cleanup := newTempDir()
325 defer cleanup()
326
327 // Create the file-based bucket.
328 bucket, err := fileblob.OpenBucket(dir, nil)
329 if err != nil {
330 log.Fatal(err)
331 }
332 defer bucket.Close()
333
334 // Create some blob objects for listing: "foo[0..7].txt".
335 ctx := context.Background()
336 for i := range 8 {
337 if err := bucket.WriteAll(ctx, fmt.Sprintf("foo%d.txt", i), []byte("Go Cloud Development Kit"), nil); err != nil {
338 log.Fatal(err)
339 }
340 }
341
342 // Iterate over them in pages.
343 // This will list the blobs created above because fileblob is strongly
344 // consistent, but is not guaranteed to work on all services.
345
346 // The first page of 3 results.
347 objs, token, err := bucket.ListPage(ctx, blob.FirstPageToken, 3, nil)
348 if err != nil {
349 log.Fatal(err)
350 }
351 for _, obj := range objs {
352 fmt.Println(obj.Key)
353 }
354 fmt.Println("END OF PAGE 1")
355
356 // The second page of 3 results.
357 objs, token, err = bucket.ListPage(ctx, token, 3, nil)
358 if err != nil {
359 log.Fatal(err)
360 }
361 for _, obj := range objs {
362 fmt.Println(obj.Key)
363 }
364 fmt.Println("END OF PAGE 2")
365
366 // The third page with the last 2 results.
367 objs, token, err = bucket.ListPage(ctx, token, 3, nil)
368 if err != nil {
369 log.Fatal(err)
370 }
371 for _, obj := range objs {
372 fmt.Println(obj.Key)
373 }
374 fmt.Println("END OF PAGE 3")
375
376 // There are no more pages, so token is now nil. Calling ListPage again will return io.EOF.
377 if token != nil {
378 fmt.Println("Token was not nil.")

Callers

nothing calls this directly

Calls 5

OpenBucketFunction · 0.92
newTempDirFunction · 0.85
WriteAllMethod · 0.80
ListPageMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected