2016-11-09 02:01:56 +01:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-11-02 18:21:05 +02:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2018-07-25 17:51:24 +03:00
|
|
|
"context"
|
2016-11-08 23:46:46 +01:00
|
|
|
"fmt"
|
2016-11-02 18:21:05 +02:00
|
|
|
"os"
|
|
|
|
|
2022-03-02 20:05:14 +09:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
2023-10-13 12:58:07 +03:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
|
2016-11-02 18:21:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// AzureBlobstoreConfig is an authentication and configuration struct containing
|
2020-06-11 04:25:32 +08:00
|
|
|
// the data needed by the Azure SDK to interact with a specific container in the
|
2016-11-02 18:21:05 +02:00
|
|
|
// blobstore.
|
|
|
|
type AzureBlobstoreConfig struct {
|
|
|
|
Account string // Account name to authorize API requests with
|
|
|
|
Token string // Access token for the above account
|
|
|
|
Container string // Blob container to upload files into
|
|
|
|
}
|
|
|
|
|
|
|
|
// AzureBlobstoreUpload uploads a local file to the Azure Blob Storage. Note, this
|
|
|
|
// method assumes a max file size of 64MB (Azure limitation). Larger files will
|
|
|
|
// need a multi API call approach implemented.
|
|
|
|
//
|
|
|
|
// See: https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx#Anchor_3
|
|
|
|
func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig) error {
|
2016-11-08 23:46:46 +01:00
|
|
|
if *DryRunFlag {
|
|
|
|
fmt.Printf("would upload %q to %s/%s/%s\n", path, config.Account, config.Container, name)
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-02 18:21:05 +02:00
|
|
|
// Create an authenticated client against the Azure cloud
|
2019-07-23 14:06:44 +02:00
|
|
|
credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-13 12:58:07 +03:00
|
|
|
a := fmt.Sprintf("https://%s.blob.core.windows.net/", config.Account)
|
|
|
|
client, err := azblob.NewClientWithSharedKeyCredential(a, credential, nil)
|
2022-03-02 20:05:14 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-02 18:21:05 +02:00
|
|
|
// Stream the file to upload into the designated blobstore container
|
|
|
|
in, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
2023-10-13 12:58:07 +03:00
|
|
|
_, err = client.UploadFile(context.Background(), config.Container, name, in, nil)
|
2018-07-25 17:51:24 +03:00
|
|
|
return err
|
2016-11-02 18:21:05 +02:00
|
|
|
}
|
2017-04-06 13:53:33 +03:00
|
|
|
|
|
|
|
// AzureBlobstoreList lists all the files contained within an azure blobstore.
|
2023-10-13 12:58:07 +03:00
|
|
|
func AzureBlobstoreList(config AzureBlobstoreConfig) ([]*container.BlobItem, error) {
|
2022-03-02 20:05:14 +09:00
|
|
|
// Create an authenticated client against the Azure cloud
|
|
|
|
credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-23 14:06:44 +02:00
|
|
|
}
|
2023-10-13 12:58:07 +03:00
|
|
|
a := fmt.Sprintf("https://%s.blob.core.windows.net/", config.Account)
|
|
|
|
client, err := azblob.NewClientWithSharedKeyCredential(a, credential, nil)
|
2022-03-02 20:05:14 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-13 12:58:07 +03:00
|
|
|
pager := client.NewListBlobsFlatPager(config.Container, nil)
|
|
|
|
|
|
|
|
var blobs []*container.BlobItem
|
|
|
|
for pager.More() {
|
|
|
|
page, err := pager.NextPage(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
blobs = append(blobs, page.Segment.BlobItems...)
|
2017-04-06 13:53:33 +03:00
|
|
|
}
|
2023-10-13 12:58:07 +03:00
|
|
|
return blobs, nil
|
2017-04-06 13:53:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AzureBlobstoreDelete iterates over a list of files to delete and removes them
|
|
|
|
// from the blobstore.
|
2023-10-13 12:58:07 +03:00
|
|
|
func AzureBlobstoreDelete(config AzureBlobstoreConfig, blobs []*container.BlobItem) error {
|
2017-04-06 13:53:33 +03:00
|
|
|
if *DryRunFlag {
|
|
|
|
for _, blob := range blobs {
|
2022-03-02 20:05:14 +09:00
|
|
|
fmt.Printf("would delete %s (%s) from %s/%s\n", *blob.Name, blob.Properties.LastModified, config.Account, config.Container)
|
2017-04-06 13:53:33 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Create an authenticated client against the Azure cloud
|
2019-07-23 14:06:44 +02:00
|
|
|
credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-13 12:58:07 +03:00
|
|
|
a := fmt.Sprintf("https://%s.blob.core.windows.net/", config.Account)
|
|
|
|
client, err := azblob.NewClientWithSharedKeyCredential(a, credential, nil)
|
2022-03-02 20:05:14 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-06 13:53:33 +03:00
|
|
|
// Iterate over the blobs and delete them
|
|
|
|
for _, blob := range blobs {
|
2023-10-13 12:58:07 +03:00
|
|
|
if _, err := client.DeleteBlob(context.Background(), config.Container, *blob.Name, nil); err != nil {
|
2017-04-06 13:53:33 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-03-02 20:05:14 +09:00
|
|
|
fmt.Printf("deleted %s (%s)\n", *blob.Name, blob.Properties.LastModified)
|
2017-04-06 13:53:33 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|