github.com/LampardNguyen234/go-ethereum@v1.10.16-0.20220117140830-b6a3b0260724/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 "net/url" 23 "os" 24 25 "github.com/Azure/azure-storage-blob-go/azblob" 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 53 pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{}) 54 55 u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", config.Account)) 56 service := azblob.NewServiceURL(*u, pipeline) 57 58 container := service.NewContainerURL(config.Container) 59 blockblob := container.NewBlockBlobURL(name) 60 61 // Stream the file to upload into the designated blobstore container 62 in, err := os.Open(path) 63 if err != nil { 64 return err 65 } 66 defer in.Close() 67 68 _, err = blockblob.Upload(context.Background(), in, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{}) 69 return err 70 } 71 72 // AzureBlobstoreList lists all the files contained within an azure blobstore. 73 func AzureBlobstoreList(config AzureBlobstoreConfig) ([]azblob.BlobItem, error) { 74 credential := azblob.NewAnonymousCredential() 75 if len(config.Token) > 0 { 76 c, err := azblob.NewSharedKeyCredential(config.Account, config.Token) 77 if err != nil { 78 return nil, err 79 } 80 credential = c 81 } 82 pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{}) 83 84 u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", config.Account)) 85 service := azblob.NewServiceURL(*u, pipeline) 86 87 var allBlobs []azblob.BlobItem 88 // List all the blobs from the container and return them 89 container := service.NewContainerURL(config.Container) 90 nextMarker := azblob.Marker{} 91 for nextMarker.NotDone() { 92 res, err := container.ListBlobsFlatSegment(context.Background(), nextMarker, azblob.ListBlobsSegmentOptions{ 93 MaxResults: 5000, // The server only gives max 5K items 94 }) 95 if err != nil { 96 return nil, err 97 } 98 allBlobs = append(allBlobs, res.Segment.BlobItems...) 99 nextMarker = res.NextMarker 100 101 } 102 return allBlobs, nil 103 } 104 105 // AzureBlobstoreDelete iterates over a list of files to delete and removes them 106 // from the blobstore. 107 func AzureBlobstoreDelete(config AzureBlobstoreConfig, blobs []azblob.BlobItem) error { 108 if *DryRunFlag { 109 for _, blob := range blobs { 110 fmt.Printf("would delete %s (%s) from %s/%s\n", blob.Name, blob.Properties.LastModified, config.Account, config.Container) 111 } 112 return nil 113 } 114 // Create an authenticated client against the Azure cloud 115 credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token) 116 if err != nil { 117 return err 118 } 119 120 pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{}) 121 122 u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", config.Account)) 123 service := azblob.NewServiceURL(*u, pipeline) 124 125 container := service.NewContainerURL(config.Container) 126 127 // Iterate over the blobs and delete them 128 for _, blob := range blobs { 129 blockblob := container.NewBlockBlobURL(blob.Name) 130 if _, err := blockblob.Delete(context.Background(), azblob.DeleteSnapshotsOptionInclude, azblob.BlobAccessConditions{}); err != nil { 131 return err 132 } 133 fmt.Printf("deleted %s (%s)\n", blob.Name, blob.Properties.LastModified) 134 } 135 return nil 136 }