github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/blob/azureblob/example_test.go (about) 1 // Copyright 2018 The Go Cloud Development Kit Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package azureblob_test 16 17 import ( 18 "context" 19 "log" 20 21 "gocloud.dev/blob" 22 "gocloud.dev/blob/azureblob" 23 ) 24 25 func ExampleOpenBucket() { 26 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 27 // PRAGMA: On gocloud.dev, hide lines until the next blank line. 28 ctx := context.Background() 29 30 const ( 31 // The storage container to access. 32 containerName = "my-container" 33 ) 34 35 // Construct the service URL. 36 // There are many forms of service URLs, see ServiceURLOptions. 37 opts := azureblob.NewDefaultServiceURLOptions() 38 serviceURL, err := azureblob.NewServiceURL(opts) 39 if err != nil { 40 log.Fatal(err) 41 } 42 43 // There are many ways to authenticate to Azure. 44 // This approach uses environment variables as described in azureblob package 45 // documentation. 46 // For example, to use shared key authentication, you would set 47 // AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_KEY. 48 // To use a SAS token, you would set AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_SAS_TOKEN. 49 // You can also construct a client using the azblob constructors directly, like 50 // azblob.NewServiceClientWithSharedKey. 51 client, err := azureblob.NewDefaultServiceClient(serviceURL) 52 if err != nil { 53 log.Fatal(err) 54 } 55 56 // Create a *blob.Bucket. 57 b, err := azureblob.OpenBucket(ctx, client, containerName, nil) 58 if err != nil { 59 log.Fatal(err) 60 } 61 defer b.Close() 62 63 // Now we can use b to read or write files to the container. 64 data, err := b.ReadAll(ctx, "my-key") 65 if err != nil { 66 log.Fatal(err) 67 } 68 _ = data 69 } 70 71 func Example_openBucketFromURL() { 72 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 73 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/blob/azureblob" 74 // PRAGMA: On gocloud.dev, hide lines until the next blank line. 75 ctx := context.Background() 76 77 // blob.OpenBucket creates a *blob.Bucket from a URL. 78 // This URL will open the container "my-container" using default 79 // credentials found in environment variables as documented in 80 // the package. 81 // Assuming AZURE_STORAGE_ACCOUNT is set to "myaccount", 82 // and other options aren't set, the service URL will look like: 83 // "https://myaccount.blob.core.windows.net/my-container". 84 bucket, err := blob.OpenBucket(ctx, "azblob://my-container") 85 if err != nil { 86 log.Fatal(err) 87 } 88 defer bucket.Close() 89 90 // Another example, against a local emulator. 91 // Assuming AZURE_STORAGE_ACCOUNT is set to "myaccount", 92 // the service URL will look like: 93 // "http://localhost:10001/myaccount/my-container". 94 localbucket, err := blob.OpenBucket(ctx, "azblob://my-container?protocol=http&domain=localhost:10001") 95 if err != nil { 96 log.Fatal(err) 97 } 98 defer localbucket.Close() 99 }