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

Function Example

blob/example_test.go:134–163  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

132}
133
134func Example() {
135 // Connect to a bucket when your program starts up.
136 // This example uses the file-based implementation in fileblob, and creates
137 // a temporary directory to use as the root directory.
138 dir, cleanup := newTempDir()
139 defer cleanup()
140 bucket, err := fileblob.OpenBucket(dir, nil)
141 if err != nil {
142 log.Fatal(err)
143 }
144 defer bucket.Close()
145
146 // We now have a *blob.Bucket! We can write our application using the
147 // *blob.Bucket type, and have the freedom to change the initialization code
148 // above to choose a different service-specific driver later.
149
150 // In this example, we'll write a blob and then read it.
151 ctx := context.Background()
152 if err := bucket.WriteAll(ctx, "foo.txt", []byte("Go Cloud Development Kit"), nil); err != nil {
153 log.Fatal(err)
154 }
155 b, err := bucket.ReadAll(ctx, "foo.txt")
156 if err != nil {
157 log.Fatal(err)
158 }
159 fmt.Println(string(b))
160
161 // Output:
162 // Go Cloud Development Kit
163}
164
165func ExampleBucket_ErrorAs() {
166 // This example is specific to the s3blob implementation; it demonstrates

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected