@@ -2,12 +2,13 @@ package getter
22
33import (
44 "fmt"
5- // "io"
5+ "io"
66 "net/url"
7- // "os"
8- // "path/filepath"
7+ "os"
8+ "path/filepath"
99 "strings"
10- //"log"
10+ "log"
11+ "bytes"
1112 //
1213 "github.com/Azure/azure-storage-blob-go/azblob"
1314)
@@ -205,44 +206,78 @@ func (g *AzureBlobGetter) GetFile(dst string, u *url.URL) error {
205206 return g .getObject (client , dst , containerName , blobPath )
206207}
207208
208- func (g * AzureBlobGetter ) getObject (client * azblob.SharedKeyCredential , dst , container , blobName string ) error {
209- //r, err := client(container, blobName)
209+ func (g * AzureBlobGetter ) getObject (serviceURL azblob.ServiceURL , dst , container , blobName string ) error {
210+ // All HTTP operations allow you to specify a Go context.Context object to control cancellation/timeout.
211+ //ctx := context.Background() // This example uses a never-expiring context.
212+
213+ // This example shows several common operations just to get you started.
214+
215+ // Create a URL that references a to-be-created container in your Azure Storage account.
216+ // This returns a ContainerURL object that wraps the container's URL and a request pipeline (inherited from serviceURL)
217+ containerURL := serviceURL .NewContainerURL (container ) // Container names require lowercase
218+
219+ //// Create the container on the service (with no metadata and no public access)
220+ //_, err = containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessNone)
210221 //if err != nil {
211- // return err
212- //}
213- //
214- //// Create all the parent directories
215- //if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
216- // return err
222+ // log.Fatal(err)
217223 //}
218- //
219- //f, err := os.Create(dst)
224+
225+ // Create a URL that references a to-be-created blob in your Azure Storage account's container.
226+ // This returns a BlockBlobURL object that wraps the blob's URL and a request pipeline (inherited from containerURL)
227+ blobURL := containerURL .NewBlockBlobURL (blobName ) // Blob names can be mixed case
228+
229+ // Download the blob's contents and verify that it worked correctly
230+ get , err := blobURL .Download (nil , 0 , 0 , azblob.BlobAccessConditions {}, false )
231+ if err != nil {
232+ log .Fatal (err )
233+ }
234+
235+ downloadedData := & bytes.Buffer {}
236+ reader := get .Body (azblob.RetryReaderOptions {})
237+ downloadedData .ReadFrom (reader )
238+ reader .Close () // The client must close the response body when finished with it
239+
240+ //r, err := client(container, blobName)
220241 //if err != nil {
221242 // return err
222243 //}
223- //defer f.Close()
224244 //
225- //_, err = io.Copy(f, r)
226- //return err
227- return nil
245+
246+ // Create all the parent directories
247+ if err := os .MkdirAll (filepath .Dir (dst ), 0755 ); err != nil {
248+ return err
249+ }
250+
251+ f , err := os .Create (dst )
252+ if err != nil {
253+ return err
254+ }
255+ defer f .Close ()
256+
257+ _ , err = io .Copy (f , downloadedData )
258+ return err
228259}
229260
230- func (g * AzureBlobGetter ) getBobClient (accountName string , baseURL string , accountKey string ) (* azblob.SharedKeyCredential , error ) {
231- var b * azblob.SharedKeyCredential
232- //
233- //if accountKey == "" {
234- // accountKey = os.Getenv("ARM_ACCESS_KEY")
235- //}
236- //
237- //c, err := storage.NewClient(accountName, accountKey, baseURL, storage.DefaultAPIVersion, true)
238- //if err != nil {
239- // return b, err
240- //}
241- //
242- //b = c.GetBlobService()
243- //
244- //return b, nil
245- return b , nil
261+ func (g * AzureBlobGetter ) getBobClient (accountName string , baseURL string , accountKey string ) (azblob.ServiceURL , error ) {
262+ // Use your Storage account's name and key to create a credential object; this is used to access your account.
263+ credential , err := azblob .NewSharedKeyCredential (accountName , accountKey )
264+ if err != nil {
265+ log .Fatal (err )
266+ }
267+
268+ // Create a request pipeline that is used to process HTTP(S) requests and responses. It requires
269+ // your account credentials. In more advanced scenarios, you can configure telemetry, retry policies,
270+ // logging, and other options. Also, you can configure multiple request pipelines for different scenarios.
271+ p := azblob .NewPipeline (credential , azblob.PipelineOptions {})
272+
273+ // From the Azure portal, get your Storage account blob service URL endpoint.
274+ // The URL typically looks like this:
275+ u , _ := url .Parse (fmt .Sprintf ("https://%s.blob.core.windows.net" , accountName ))
276+
277+ // Create an ServiceURL object that wraps the service URL and a request pipeline.
278+ serviceURL := azblob .NewServiceURL (* u , p )
279+
280+ return serviceURL , nil
246281}
247282
248283func (g * AzureBlobGetter ) parseUrl (u * url.URL ) (accountName , baseURL , container , blobPath , accessKey string , err error ) {
0 commit comments