github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/internal/build/azure.go (about)

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