github.com/ethereum/go-ethereum@v1.14.3/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  	"context"
    21  	"fmt"
    22  	"os"
    23  
    24  	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
    25  	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
    26  )
    27  
    28  // AzureBlobstoreConfig is an authentication and configuration struct containing
    29  // the data needed by the Azure SDK to interact with a specific container in the
    30  // blobstore.
    31  type AzureBlobstoreConfig struct {
    32  	Account   string // Account name to authorize API requests with
    33  	Token     string // Access token for the above account
    34  	Container string // Blob container to upload files into
    35  }
    36  
    37  // AzureBlobstoreUpload uploads a local file to the Azure Blob Storage. Note, this
    38  // method assumes a max file size of 64MB (Azure limitation). Larger files will
    39  // need a multi API call approach implemented.
    40  //
    41  // See: https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx#Anchor_3
    42  func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig) error {
    43  	if *DryRunFlag {
    44  		fmt.Printf("would upload %q to %s/%s/%s\n", path, config.Account, config.Container, name)
    45  		return nil
    46  	}
    47  	// Create an authenticated client against the Azure cloud
    48  	credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	a := fmt.Sprintf("https://%s.blob.core.windows.net/", config.Account)
    53  	client, err := azblob.NewClientWithSharedKeyCredential(a, credential, nil)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	// Stream the file to upload into the designated blobstore container
    58  	in, err := os.Open(path)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	defer in.Close()
    63  
    64  	_, err = client.UploadFile(context.Background(), config.Container, name, in, nil)
    65  	return err
    66  }
    67  
    68  // AzureBlobstoreList lists all the files contained within an azure blobstore.
    69  func AzureBlobstoreList(config AzureBlobstoreConfig) ([]*container.BlobItem, error) {
    70  	// Create an authenticated client against the Azure cloud
    71  	credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	a := fmt.Sprintf("https://%s.blob.core.windows.net/", config.Account)
    76  	client, err := azblob.NewClientWithSharedKeyCredential(a, credential, nil)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	pager := client.NewListBlobsFlatPager(config.Container, nil)
    81  
    82  	var blobs []*container.BlobItem
    83  	for pager.More() {
    84  		page, err := pager.NextPage(context.TODO())
    85  		if err != nil {
    86  			return nil, err
    87  		}
    88  		blobs = append(blobs, page.Segment.BlobItems...)
    89  	}
    90  	return blobs, nil
    91  }
    92  
    93  // AzureBlobstoreDelete iterates over a list of files to delete and removes them
    94  // from the blobstore.
    95  func AzureBlobstoreDelete(config AzureBlobstoreConfig, blobs []*container.BlobItem) error {
    96  	if *DryRunFlag {
    97  		for _, blob := range blobs {
    98  			fmt.Printf("would delete %s (%s) from %s/%s\n", *blob.Name, blob.Properties.LastModified, config.Account, config.Container)
    99  		}
   100  		return nil
   101  	}
   102  	// Create an authenticated client against the Azure cloud
   103  	credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	a := fmt.Sprintf("https://%s.blob.core.windows.net/", config.Account)
   108  	client, err := azblob.NewClientWithSharedKeyCredential(a, credential, nil)
   109  	if err != nil {
   110  		return err
   111  	}
   112  	// Iterate over the blobs and delete them
   113  	for _, blob := range blobs {
   114  		if _, err := client.DeleteBlob(context.Background(), config.Container, *blob.Name, nil); err != nil {
   115  			return err
   116  		}
   117  		fmt.Printf("deleted  %s (%s)\n", *blob.Name, blob.Properties.LastModified)
   118  	}
   119  	return nil
   120  }