summaryrefslogtreecommitdiff
path: root/blobby/main.go
blob: 5b6f89f8031c4afe42716afabd50a2eb617c13ff (plain)
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
package main

import (
	"gocloud.dev/blob"
	// _ "gocloud.dev/blob/<driver>"
	"context"
	"fmt"

	_ "gocloud.dev/blob/mysql"
)

func main() {
	bucket, err := blob.OpenBucket(context.Background(), "<driver-url>")
	if err != nil {
		return fmt.Errorf("could not open bucket: %v", err)
	}
	defer bucket.Close()
	// bucket is a *blob.Bucket; see usage below

	// You can wrap a *blob.Bucket to always operate on a subfolder of the bucket using blob.PrefixedBucket:

	// import "gocloud.dev/blob"

	// 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>".

	// Alternatively, you can configure the prefix directly in the blob.OpenBucket URL:

	/*
	   import (
	   	"context"
	   	"gocloud.dev/blob"
	   )
	*/

	// Connect to a bucket using a URL, using the "prefix" query parameter to
	// target a subfolder in the bucket.
	// The prefix should end with "/", so that the resulting bucket operates
	// in a subfolder.
	b, err := blob.OpenBucket(ctx, "mem://?prefix=a/subfolder/")
	if err != nil {
		return err
	}
	defer b.Close()

	// Bucket operations on <key> will be translated to "a/subfolder/<key>".
}