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

Function ExampleBucket_List_withDelimiter

blob/example_test.go:259–319  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

257}
258
259func ExampleBucket_List_withDelimiter() {
260 // Connect to a bucket when your program starts up.
261 // This example uses the file-based implementation.
262 dir, cleanup := newTempDir()
263 defer cleanup()
264
265 // Create the file-based bucket.
266 bucket, err := fileblob.OpenBucket(dir, nil)
267 if err != nil {
268 log.Fatal(err)
269 }
270 defer bucket.Close()
271
272 // Create some blob objects in a hierarchy.
273 ctx := context.Background()
274 for _, key := range []string{
275 "dir1/subdir/a.txt",
276 "dir1/subdir/b.txt",
277 "dir2/c.txt",
278 "d.txt",
279 } {
280 if err := bucket.WriteAll(ctx, key, []byte("Go Cloud Development Kit"), nil); err != nil {
281 log.Fatal(err)
282 }
283 }
284
285 // list lists files in b starting with prefix. It uses the delimiter "/",
286 // and recurses into "directories", adding 2 spaces to indent each time.
287 // It will list the blobs created above because fileblob is strongly
288 // consistent, but is not guaranteed to work on all services.
289 var list func(context.Context, *blob.Bucket, string, string)
290 list = func(ctx context.Context, b *blob.Bucket, prefix, indent string) {
291 iter := b.List(&blob.ListOptions{
292 Delimiter: "/",
293 Prefix: prefix,
294 })
295 for {
296 obj, err := iter.Next(ctx)
297 if err == io.EOF {
298 break
299 }
300 if err != nil {
301 log.Fatal(err)
302 }
303 fmt.Printf("%s%s\n", indent, obj.Key)
304 if obj.IsDir {
305 list(ctx, b, obj.Key, indent+" ")
306 }
307 }
308 }
309 list(ctx, bucket, "", "")
310
311 // Output:
312 // d.txt
313 // dir1/
314 // dir1/subdir/
315 // dir1/subdir/a.txt
316 // dir1/subdir/b.txt

Callers

nothing calls this directly

Calls 6

OpenBucketFunction · 0.92
newTempDirFunction · 0.85
WriteAllMethod · 0.80
ListMethod · 0.80
CloseMethod · 0.65
NextMethod · 0.65

Tested by

no test coverage detected