()
| 190 | } |
| 191 | |
| 192 | func ExampleBucket_List() { |
| 193 | // Connect to a bucket when your program starts up. |
| 194 | // This example uses the file-based implementation. |
| 195 | dir, cleanup := newTempDir() |
| 196 | defer cleanup() |
| 197 | |
| 198 | // Create the file-based bucket. |
| 199 | bucket, err := fileblob.OpenBucket(dir, nil) |
| 200 | if err != nil { |
| 201 | log.Fatal(err) |
| 202 | } |
| 203 | defer bucket.Close() |
| 204 | |
| 205 | // Create some blob objects for listing: "foo[0..4].txt". |
| 206 | ctx := context.Background() |
| 207 | for i := range 5 { |
| 208 | if err := bucket.WriteAll(ctx, fmt.Sprintf("foo%d.txt", i), []byte("Go Cloud Development Kit"), nil); err != nil { |
| 209 | log.Fatal(err) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Iterate over them. |
| 214 | // This will list the blobs created above because fileblob is strongly |
| 215 | // consistent, but is not guaranteed to work on all services. |
| 216 | li := bucket.List(nil) |
| 217 | for { |
| 218 | obj, err := li.Next(ctx) |
| 219 | if err == io.EOF { |
| 220 | break |
| 221 | } |
| 222 | if err != nil { |
| 223 | log.Fatal(err) |
| 224 | } |
| 225 | fmt.Println(obj.Key) |
| 226 | } |
| 227 | |
| 228 | // Alternatively, use All to iterate (and optionally download): |
| 229 | fmt.Println() |
| 230 | fmt.Println("Now, using an iterator:") |
| 231 | li = bucket.List(nil) |
| 232 | iter, errFn := li.All(ctx) |
| 233 | for obj, download := range iter { |
| 234 | var buf bytes.Buffer |
| 235 | if err := download(&buf, nil /* default ReaderOptions */); err != nil { |
| 236 | log.Fatalf("download of %q failed: %v", obj.Key, err) |
| 237 | } |
| 238 | fmt.Printf("%s: %s\n", obj.Key, string(buf.Bytes())) |
| 239 | } |
| 240 | if err := errFn(); err != nil { |
| 241 | log.Fatalf("iteration failed: %v", err) |
| 242 | } |
| 243 | |
| 244 | // Output: |
| 245 | // foo0.txt |
| 246 | // foo1.txt |
| 247 | // foo2.txt |
| 248 | // foo3.txt |
| 249 | // foo4.txt |
nothing calls this directly
no test coverage detected