github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/internal/build/azure.go (about) 1 package build 2 3 import ( 4 "fmt" 5 "os" 6 7 storage "github.com/Azure/azure-storage-go" 8 ) 9 10 // AzureBlobstoreConfig is an authentication and configuration struct containing 11 // the data needed by the Azure SDK to interact with a speicifc container in the 12 // blobstore. 13 type AzureBlobstoreConfig struct { 14 Account string // Account name to authorize API requests with 15 Token string // Access token for the above account 16 Container string // Blob container to upload files into 17 } 18 19 // AzureBlobstoreUpload uploads a local file to the Azure Blob Storage. Note, this 20 // method assumes a max file size of 64MB (Azure limitation). Larger files will 21 // need a multi API call approach implemented. 22 // 23 // See: https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx#Anchor_3 24 func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig) error { 25 if *DryRunFlag { 26 fmt.Printf("would upload %q to %s/%s/%s\n", path, config.Account, config.Container, name) 27 return nil 28 } 29 // Create an authenticated client against the Azure cloud 30 rawClient, err := storage.NewBasicClient(config.Account, config.Token) 31 if err != nil { 32 return err 33 } 34 client := rawClient.GetBlobService() 35 36 // Stream the file to upload into the designated blobstore container 37 in, err := os.Open(path) 38 if err != nil { 39 return err 40 } 41 defer in.Close() 42 43 info, err := in.Stat() 44 if err != nil { 45 return err 46 } 47 return client.CreateBlockBlobFromReader(config.Container, name, uint64(info.Size()), in, nil) 48 } 49 50 // AzureBlobstoreList lists all the files contained within an azure blobstore. 51 func AzureBlobstoreList(config AzureBlobstoreConfig) ([]storage.Blob, error) { 52 // Create an authenticated client against the Azure cloud 53 rawClient, err := storage.NewBasicClient(config.Account, config.Token) 54 if err != nil { 55 return nil, err 56 } 57 client := rawClient.GetBlobService() 58 59 // List all the blobs from the container and return them 60 container := client.GetContainerReference(config.Container) 61 62 blobs, err := container.ListBlobs(storage.ListBlobsParameters{ 63 MaxResults: 1024 * 1024 * 1024, // Yes, fetch all of them 64 Timeout: 3600, // Yes, wait for all of them 65 }) 66 if err != nil { 67 return nil, err 68 } 69 return blobs.Blobs, nil 70 } 71 72 // AzureBlobstoreDelete iterates over a list of files to delete and removes them 73 // from the blobstore. 74 func AzureBlobstoreDelete(config AzureBlobstoreConfig, blobs []storage.Blob) error { 75 if *DryRunFlag { 76 for _, blob := range blobs { 77 fmt.Printf("would delete %s (%s) from %s/%s\n", blob.Name, blob.Properties.LastModified, config.Account, config.Container) 78 } 79 return nil 80 } 81 // Create an authenticated client against the Azure cloud 82 rawClient, err := storage.NewBasicClient(config.Account, config.Token) 83 if err != nil { 84 return err 85 } 86 client := rawClient.GetBlobService() 87 88 // Iterate over the blobs and delete them 89 for _, blob := range blobs { 90 if err := client.DeleteBlob(config.Container, blob.Name, nil); err != nil { 91 return err 92 } 93 } 94 return nil 95 }