github.com/cornelk/go-cloud@v0.17.1/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 "github.com/Azure/azure-storage-blob-go/azblob" 22 "github.com/Azure/go-autorest/autorest/azure/auth" 23 "github.com/cornelk/go-cloud/blob" 24 "github.com/cornelk/go-cloud/blob/azureblob" 25 ) 26 27 func ExampleOpenBucket() { 28 // PRAGMA: This example is used on github.com/cornelk/go-cloud; PRAGMA comments adjust how it is shown and can be ignored. 29 // PRAGMA: On github.com/cornelk/go-cloud, hide lines until the next blank line. 30 ctx := context.Background() 31 32 const ( 33 // Fill in with your Azure Storage Account and Access Key. 34 accountName azureblob.AccountName = "my-account" 35 accountKey azureblob.AccountKey = "my-account-key" 36 // Fill in with the storage container to access. 37 containerName = "my-container" 38 ) 39 40 // Create a credentials object. 41 credential, err := azureblob.NewCredential(accountName, accountKey) 42 if err != nil { 43 log.Fatal(err) 44 } 45 46 // Create a Pipeline, using whatever PipelineOptions you need. 47 pipeline := azureblob.NewPipeline(credential, azblob.PipelineOptions{}) 48 49 // Create a *blob.Bucket. 50 // The credential Option is required if you're going to use blob.SignedURL. 51 bucket, err := azureblob.OpenBucket(ctx, pipeline, accountName, containerName, 52 &azureblob.Options{Credential: credential}) 53 if err != nil { 54 log.Fatal(err) 55 } 56 defer bucket.Close() 57 } 58 59 func ExampleOpenBucket_usingSASToken() { 60 const ( 61 // Your Azure Storage Account and SASToken. 62 accountName = azureblob.AccountName("my-account") 63 sasToken = azureblob.SASToken("my-SAS-token") 64 // The storage container to access. 65 containerName = "my-container" 66 ) 67 68 // Since we're using a SASToken, we can use anonymous credentials. 69 credential := azblob.NewAnonymousCredential() 70 71 // Create a Pipeline, using whatever PipelineOptions you need. 72 pipeline := azureblob.NewPipeline(credential, azblob.PipelineOptions{}) 73 74 // Create a *blob.Bucket. 75 // Note that we're not supplying azureblob.Options.Credential, so SignedURL 76 // won't work. To use SignedURL, you need a real credential (see the other 77 // example). 78 ctx := context.Background() 79 b, err := azureblob.OpenBucket(ctx, pipeline, accountName, containerName, &azureblob.Options{SASToken: sasToken}) 80 if err != nil { 81 log.Fatal(err) 82 } 83 defer b.Close() 84 85 // Now we can use b to read or write files to the container. 86 data, err := b.ReadAll(ctx, "my-key") 87 if err != nil { 88 log.Fatal(err) 89 } 90 _ = data 91 } 92 93 func Example_openBucketFromURL() { 94 // PRAGMA: This example is used on github.com/cornelk/go-cloud; PRAGMA comments adjust how it is shown and can be ignored. 95 // PRAGMA: On github.com/cornelk/go-cloud, add a blank import: _ "github.com/cornelk/go-cloud/blob/azureblob" 96 // PRAGMA: On github.com/cornelk/go-cloud, hide lines until the next blank line. 97 ctx := context.Background() 98 99 // blob.OpenBucket creates a *blob.Bucket from a URL. 100 // This URL will open the container "my-container" using default 101 // credentials found in the environment variables 102 // AZURE_STORAGE_ACCOUNT plus at least one of AZURE_STORAGE_KEY 103 // and AZURE_STORAGE_SAS_TOKEN. 104 bucket, err := blob.OpenBucket(ctx, "azblob://my-container") 105 if err != nil { 106 log.Fatal(err) 107 } 108 defer bucket.Close() 109 } 110 111 func ExampleOpenBucket_usingAADCredentials() { 112 const ( 113 // Your Azure Storage Account. 114 accountName = azureblob.AccountName("my-account") 115 116 // Your Azure AAD Service Principal with access to the storage account. 117 // https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad-app 118 clientID = "123" 119 clientSecret = "456" 120 tenantID = "789" 121 122 // The storage container to access. 123 containerName = "my-container" 124 ) 125 126 // Get an Oauth2 token for the account for use with Azure Storage. 127 ccc := auth.NewClientCredentialsConfig(clientID, clientSecret, tenantID) 128 129 // Set the target resource to the Azure storage. This is available as a 130 // constant using "azure.PublicCloud.ResourceIdentifiers.Storage". 131 ccc.Resource = "https://storage.azure.com/" 132 token, err := ccc.ServicePrincipalToken() 133 if err != nil { 134 log.Fatal(err) 135 } 136 137 // Refresh OAuth2 token. 138 if err := token.RefreshWithContext(context.Background()); err != nil { 139 log.Fatal(err) 140 } 141 142 // Create the credential using the OAuth2 token. 143 credential := azblob.NewTokenCredential(token.OAuthToken(), nil) 144 145 // Create a Pipeline, using whatever PipelineOptions you need. 146 pipeline := azureblob.NewPipeline(credential, azblob.PipelineOptions{}) 147 148 // Create a *blob.Bucket. 149 // Note that we're not supplying azureblob.Options.Credential, so SignedURL 150 // won't work. To use SignedURL, you need a real credential (see the other 151 // example). 152 ctx := context.Background() 153 b, err := azureblob.OpenBucket(ctx, pipeline, accountName, containerName, new(azureblob.Options)) 154 if err != nil { 155 log.Fatal(err) 156 } 157 defer b.Close() 158 }