github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/internal/build/azure.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package build
    13  
    14  import (
    15  	"fmt"
    16  	"os"
    17  
    18  	storage "github.com/Azure/azure-storage-go"
    19  )
    20  
    21  // AzureBlobstoreConfig is an authentication and configuration struct containing
    22  // the data needed by the Azure SDK to interact with a speicifc container in the
    23  // blobstore.
    24  type AzureBlobstoreConfig struct {
    25  	Account   string // Account name to authorize API requests with
    26  	Token     string // Access token for the above account
    27  	Container string // Blob container to upload files into
    28  }
    29  
    30  // AzureBlobstoreUpload uploads a local file to the Azure Blob Storage. Note, this
    31  // method assumes a max file size of 64MB (Azure limitation). Larger files will
    32  // need a multi API call approach implemented.
    33  //
    34  // See: https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx#Anchor_3
    35  func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig) error {
    36  	if *DryRunFlag {
    37  		fmt.Printf("would upload %q to %s/%s/%s\n", path, config.Account, config.Container, name)
    38  		return nil
    39  	}
    40  	// Create an authenticated client against the Azure cloud
    41  	rawClient, err := storage.NewBasicClient(config.Account, config.Token)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	client := rawClient.GetBlobService()
    46  
    47  	// Stream the file to upload into the designated blobstore container
    48  	in, err := os.Open(path)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	defer in.Close()
    53  
    54  	info, err := in.Stat()
    55  	if err != nil {
    56  		return err
    57  	}
    58  	return client.CreateBlockBlobFromReader(config.Container, name, uint64(info.Size()), in, nil)
    59  }
    60  
    61  // AzureBlobstoreList lists all the files contained within an azure blobstore.
    62  func AzureBlobstoreList(config AzureBlobstoreConfig) ([]storage.Blob, error) {
    63  	// Create an authenticated client against the Azure cloud
    64  	rawClient, err := storage.NewBasicClient(config.Account, config.Token)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	client := rawClient.GetBlobService()
    69  
    70  	// List all the blobs from the container and return them
    71  	container := client.GetContainerReference(config.Container)
    72  
    73  	blobs, err := container.ListBlobs(storage.ListBlobsParameters{
    74  		MaxResults: 1024 * 1024 * 1024, // Yes, fetch all of them
    75  		Timeout:    3600,               // Yes, wait for all of them
    76  	})
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	return blobs.Blobs, nil
    81  }
    82  
    83  // AzureBlobstoreDelete iterates over a list of files to delete and removes them
    84  // from the blobstore.
    85  func AzureBlobstoreDelete(config AzureBlobstoreConfig, blobs []storage.Blob) error {
    86  	if *DryRunFlag {
    87  		for _, blob := range blobs {
    88  			fmt.Printf("would delete %s (%s) from %s/%s\n", blob.Name, blob.Properties.LastModified, config.Account, config.Container)
    89  		}
    90  		return nil
    91  	}
    92  	// Create an authenticated client against the Azure cloud
    93  	rawClient, err := storage.NewBasicClient(config.Account, config.Token)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	client := rawClient.GetBlobService()
    98  
    99  	// Iterate over the blobs and delete them
   100  	for _, blob := range blobs {
   101  		if err := client.DeleteBlob(config.Container, blob.Name, nil); err != nil {
   102  			return err
   103  		}
   104  	}
   105  	return nil
   106  }