1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
|
// Copyright 2018 The Go Cloud Development Kit Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package blob_test
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"cloud.google.com/go/storage"
"github.com/aws/aws-sdk-go/aws/awserr"
"gocloud.dev/blob"
"gocloud.dev/blob/fileblob"
_ "gocloud.dev/blob/gcsblob"
_ "gocloud.dev/blob/s3blob"
)
func ExampleBucket_NewReader() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()
var bucket *blob.Bucket
// Open the key "foo.txt" for reading with the default options.
r, err := bucket.NewReader(ctx, "foo.txt", nil)
if err != nil {
log.Fatal(err)
}
defer r.Close()
// Readers also have a limited view of the blob's metadata.
fmt.Println("Content-Type:", r.ContentType())
fmt.Println()
// Copy from the reader to stdout.
if _, err := io.Copy(os.Stdout, r); err != nil {
log.Fatal(err)
}
}
func ExampleBucket_NewRangeReader() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()
var bucket *blob.Bucket
// Open the key "foo.txt" for reading at offset 1024 and read up to 4096 bytes.
r, err := bucket.NewRangeReader(ctx, "foo.txt", 1024, 4096, nil)
if err != nil {
log.Fatal(err)
}
defer r.Close()
// Copy from the read range to stdout.
if _, err := io.Copy(os.Stdout, r); err != nil {
log.Fatal(err)
}
}
func ExampleBucket_NewWriter() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()
var bucket *blob.Bucket
// Open the key "foo.txt" for writing with the default options.
w, err := bucket.NewWriter(ctx, "foo.txt", nil)
if err != nil {
log.Fatal(err)
}
_, writeErr := fmt.Fprintln(w, "Hello, World!")
// Always check the return value of Close when writing.
closeErr := w.Close()
if writeErr != nil {
log.Fatal(writeErr)
}
if closeErr != nil {
log.Fatal(closeErr)
}
}
func ExampleBucket_NewWriter_cancel() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()
var bucket *blob.Bucket
// Create a cancelable context from the existing context.
writeCtx, cancelWrite := context.WithCancel(ctx)
defer cancelWrite()
// Open the key "foo.txt" for writing with the default options.
w, err := bucket.NewWriter(writeCtx, "foo.txt", nil)
if err != nil {
log.Fatal(err)
}
// Assume some writes happened and we encountered an error.
// Now we want to abort the write.
if err != nil {
// First cancel the context.
cancelWrite()
// You must still close the writer to avoid leaking resources.
w.Close()
}
}
func ExampleBucket_Delete() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()
var bucket *blob.Bucket
if err := bucket.Delete(ctx, "foo.txt"); err != nil {
log.Fatal(err)
}
}
func Example() {
// Connect to a bucket when your program starts up.
// This example uses the file-based implementation in fileblob, and creates
// a temporary directory to use as the root directory.
dir, cleanup := newTempDir()
defer cleanup()
bucket, err := fileblob.OpenBucket(dir, nil)
if err != nil {
log.Fatal(err)
}
defer bucket.Close()
// We now have a *blob.Bucket! We can write our application using the
// *blob.Bucket type, and have the freedom to change the initialization code
// above to choose a different service-specific driver later.
// In this example, we'll write a blob and then read it.
ctx := context.Background()
if err := bucket.WriteAll(ctx, "foo.txt", []byte("Go Cloud Development Kit"), nil); err != nil {
log.Fatal(err)
}
b, err := bucket.ReadAll(ctx, "foo.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
// Output:
// Go Cloud Development Kit
}
func ExampleBucket_ErrorAs() {
// This example is specific to the s3blob implementation; it demonstrates
// access to the underlying awserr.Error type.
// The types exposed for ErrorAs by s3blob are documented in
// https://godoc.org/gocloud.dev/blob/s3blob#hdr-As
ctx := context.Background()
b, err := blob.OpenBucket(ctx, "s3://my-bucket")
if err != nil {
log.Fatal(err)
}
defer b.Close()
_, err = b.ReadAll(ctx, "nosuchfile")
if err != nil {
var awsErr awserr.Error
if b.ErrorAs(err, &awsErr) {
fmt.Println(awsErr.Code())
}
}
}
func ExampleBucket_List() {
// Connect to a bucket when your program starts up.
// This example uses the file-based implementation.
dir, cleanup := newTempDir()
defer cleanup()
// Create the file-based bucket.
bucket, err := fileblob.OpenBucket(dir, nil)
if err != nil {
log.Fatal(err)
}
defer bucket.Close()
// Create some blob objects for listing: "foo[0..4].txt".
ctx := context.Background()
for i := 0; i < 5; i++ {
if err := bucket.WriteAll(ctx, fmt.Sprintf("foo%d.txt", i), []byte("Go Cloud Development Kit"), nil); err != nil {
log.Fatal(err)
}
}
// Iterate over them.
// This will list the blobs created above because fileblob is strongly
// consistent, but is not guaranteed to work on all services.
iter := bucket.List(nil)
for {
obj, err := iter.Next(ctx)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(obj.Key)
}
// Output:
// foo0.txt
// foo1.txt
// foo2.txt
// foo3.txt
// foo4.txt
}
func ExampleBucket_List_withDelimiter() {
// Connect to a bucket when your program starts up.
// This example uses the file-based implementation.
dir, cleanup := newTempDir()
defer cleanup()
// Create the file-based bucket.
bucket, err := fileblob.OpenBucket(dir, nil)
if err != nil {
log.Fatal(err)
}
defer bucket.Close()
// Create some blob objects in a hierarchy.
ctx := context.Background()
for _, key := range []string{
"dir1/subdir/a.txt",
"dir1/subdir/b.txt",
"dir2/c.txt",
"d.txt",
} {
if err := bucket.WriteAll(ctx, key, []byte("Go Cloud Development Kit"), nil); err != nil {
log.Fatal(err)
}
}
// list lists files in b starting with prefix. It uses the delimiter "/",
// and recurses into "directories", adding 2 spaces to indent each time.
// It will list the blobs created above because fileblob is strongly
// consistent, but is not guaranteed to work on all services.
var list func(context.Context, *blob.Bucket, string, string)
list = func(ctx context.Context, b *blob.Bucket, prefix, indent string) {
iter := b.List(&blob.ListOptions{
Delimiter: "/",
Prefix: prefix,
})
for {
obj, err := iter.Next(ctx)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s%s\n", indent, obj.Key)
if obj.IsDir {
list(ctx, b, obj.Key, indent+" ")
}
}
}
list(ctx, bucket, "", "")
// Output:
// d.txt
// dir1/
// dir1/subdir/
// dir1/subdir/a.txt
// dir1/subdir/b.txt
// dir2/
// dir2/c.txt
}
func ExampleBucket_ListPage() {
// Connect to a bucket when your program starts up.
// This example uses the file-based implementation.
dir, cleanup := newTempDir()
defer cleanup()
// Create the file-based bucket.
bucket, err := fileblob.OpenBucket(dir, nil)
if err != nil {
log.Fatal(err)
}
defer bucket.Close()
// Create some blob objects for listing: "foo[0..7].txt".
ctx := context.Background()
for i := 0; i < 8; i++ {
if err := bucket.WriteAll(ctx, fmt.Sprintf("foo%d.txt", i), []byte("Go Cloud Development Kit"), nil); err != nil {
log.Fatal(err)
}
}
// Iterate over them in pages.
// This will list the blobs created above because fileblob is strongly
// consistent, but is not guaranteed to work on all services.
// The first page of 3 results.
objs, token, err := bucket.ListPage(ctx, blob.FirstPageToken, 3, nil)
if err != nil {
log.Fatal(err)
}
for _, obj := range objs {
fmt.Println(obj.Key)
}
fmt.Println("END OF PAGE 1")
// The second page of 3 results.
objs, token, err = bucket.ListPage(ctx, token, 3, nil)
if err != nil {
log.Fatal(err)
}
for _, obj := range objs {
fmt.Println(obj.Key)
}
fmt.Println("END OF PAGE 2")
// The third page with the last 2 results.
objs, token, err = bucket.ListPage(ctx, token, 3, nil)
if err != nil {
log.Fatal(err)
}
for _, obj := range objs {
fmt.Println(obj.Key)
}
fmt.Println("END OF PAGE 3")
// There are no more pages, so token is now nil. Calling ListPage again will return io.EOF.
if token != nil {
fmt.Println("Token was not nil.")
}
// Output:
// foo0.txt
// foo1.txt
// foo2.txt
// END OF PAGE 1
// foo3.txt
// foo4.txt
// foo5.txt
// END OF PAGE 2
// foo6.txt
// foo7.txt
// END OF PAGE 3
}
func ExampleBucket_As() {
// This example is specific to the gcsblob implementation; it demonstrates
// access to the underlying cloud.google.com/go/storage.Client type.
// The types exposed for As by gcsblob are documented in
// https://godoc.org/gocloud.dev/blob/gcsblob#hdr-As
// This URL will open the bucket "my-bucket" using default credentials.
ctx := context.Background()
b, err := blob.OpenBucket(ctx, "gs://my-bucket")
if err != nil {
log.Fatal(err)
}
defer b.Close()
// Access storage.Client fields via gcsClient here.
var gcsClient *storage.Client
if b.As(&gcsClient) {
email, err := gcsClient.ServiceAccount(ctx, "project-name")
if err != nil {
log.Fatal(err)
}
_ = email
} else {
log.Println("Unable to access storage.Client through Bucket.As")
}
}
func ExampleWriterOptions() {
// This example is specific to the gcsblob implementation; it demonstrates
// access to the underlying cloud.google.com/go/storage.Writer type.
// The types exposed for As by gcsblob are documented in
// https://godoc.org/gocloud.dev/blob/gcsblob#hdr-As
ctx := context.Background()
b, err := blob.OpenBucket(ctx, "gs://my-bucket")
if err != nil {
log.Fatal(err)
}
defer b.Close()
beforeWrite := func(as func(interface{}) bool) error {
var sw *storage.Writer
if as(&sw) {
fmt.Println(sw.ChunkSize)
}
return nil
}
options := blob.WriterOptions{BeforeWrite: beforeWrite}
if err := b.WriteAll(ctx, "newfile.txt", []byte("hello\n"), &options); err != nil {
log.Fatal(err)
}
}
func ExampleListObject_As() {
// This example is specific to the gcsblob implementation; it demonstrates
// access to the underlying cloud.google.com/go/storage.ObjectAttrs type.
// The types exposed for As by gcsblob are documented in
// https://godoc.org/gocloud.dev/blob/gcsblob#hdr-As
ctx := context.Background()
b, err := blob.OpenBucket(ctx, "gs://my-bucket")
if err != nil {
log.Fatal(err)
}
defer b.Close()
iter := b.List(nil)
for {
obj, err := iter.Next(ctx)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
// Access storage.ObjectAttrs via oa here.
var oa storage.ObjectAttrs
if obj.As(&oa) {
_ = oa.Owner
}
}
}
func ExampleListOptions() {
// This example is specific to the gcsblob implementation; it demonstrates
// access to the underlying cloud.google.com/go/storage.Query type.
// The types exposed for As by gcsblob are documented in
// https://godoc.org/gocloud.dev/blob/gcsblob#hdr-As
ctx := context.Background()
b, err := blob.OpenBucket(ctx, "gs://my-bucket")
if err != nil {
log.Fatal(err)
}
defer b.Close()
beforeList := func(as func(interface{}) bool) error {
// Access storage.Query via q here.
var q *storage.Query
if as(&q) {
_ = q.Delimiter
}
return nil
}
iter := b.List(&blob.ListOptions{Prefix: "", Delimiter: "/", BeforeList: beforeList})
for {
obj, err := iter.Next(ctx)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
_ = obj
}
}
func ExamplePrefixedBucket() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
var bucket *blob.Bucket
// Wrap the bucket using blob.PrefixedBucket.
// The prefix should end with "/", so that the resulting bucket operates
// in a subfolder.
bucket = blob.PrefixedBucket(bucket, "a/subfolder/")
// The original bucket is no longer usable; it has been closed.
// The wrapped bucket should be closed when done.
defer bucket.Close()
// Bucket operations on <key> will be translated to "a/subfolder/<key>".
}
func ExampleReader_As() {
// This example is specific to the gcsblob implementation; it demonstrates
// access to the underlying cloud.google.com/go/storage.Reader type.
// The types exposed for As by gcsblob are documented in
// https://godoc.org/gocloud.dev/blob/gcsblob#hdr-As
ctx := context.Background()
b, err := blob.OpenBucket(ctx, "gs://my-bucket")
if err != nil {
log.Fatal(err)
}
defer b.Close()
r, err := b.NewReader(ctx, "gopher.png", nil)
if err != nil {
log.Fatal(err)
}
defer r.Close()
// Access storage.Reader via sr here.
var sr *storage.Reader
if r.As(&sr) {
_ = sr.Attrs
}
}
func ExampleAttributes_As() {
// This example is specific to the gcsblob implementation; it demonstrates
// access to the underlying cloud.google.com/go/storage.ObjectAttrs type.
// The types exposed for As by gcsblob are documented in
// https://godoc.org/gocloud.dev/blob/gcsblob#hdr-As
ctx := context.Background()
b, err := blob.OpenBucket(ctx, "gs://my-bucket")
if err != nil {
log.Fatal(err)
}
defer b.Close()
attrs, err := b.Attributes(ctx, "gopher.png")
if err != nil {
log.Fatal(err)
}
var oa storage.ObjectAttrs
if attrs.As(&oa) {
fmt.Println(oa.Owner)
}
}
func newTempDir() (string, func()) {
dir, err := ioutil.TempDir("", "go-cloud-blob-example")
if err != nil {
panic(err)
}
return dir, func() { os.RemoveAll(dir) }
}
|