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