mirror of
https://github.com/offen/docker-volume-backup.git
synced 2024-11-15 02:40:27 +01:00
Implement copy for Azure Blob Storage
This commit is contained in:
parent
e702b2b682
commit
7aa2166aee
@ -66,6 +66,7 @@ type Config struct {
|
|||||||
AzureStorageAccountName string `split_words:"true"`
|
AzureStorageAccountName string `split_words:"true"`
|
||||||
AzureStoragePrimaryAccountKey string `split_words:"true"`
|
AzureStoragePrimaryAccountKey string `split_words:"true"`
|
||||||
AzureStorageContainerName string `split_words:"true"`
|
AzureStorageContainerName string `split_words:"true"`
|
||||||
|
AzureStorageEndpoint string `split_words:"true" default:""https://%s.blob.core.windows.net/""`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) resolveSecret(envVar string, secretPath string) (string, error) {
|
func (c *Config) resolveSecret(envVar string, secretPath string) (string, error) {
|
||||||
|
@ -4,13 +4,19 @@
|
|||||||
package azure
|
package azure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
||||||
"github.com/offen/docker-volume-backup/internal/storage"
|
"github.com/offen/docker-volume-backup/internal/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
type azureBlobStorage struct {
|
type azureBlobStorage struct {
|
||||||
*storage.StorageBackend
|
*storage.StorageBackend
|
||||||
|
client *azblob.Client
|
||||||
|
containerName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config contains values that define the configuration of an Azure Blob Storage.
|
// Config contains values that define the configuration of an Azure Blob Storage.
|
||||||
@ -18,21 +24,46 @@ type Config struct {
|
|||||||
AccountName string
|
AccountName string
|
||||||
ContainerName string
|
ContainerName string
|
||||||
PrimaryAccountKey string
|
PrimaryAccountKey string
|
||||||
|
Endpoint string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStorageBackend creates and initializes a new S3/Minio storage backend.
|
// NewStorageBackend creates and initializes a new Azure Blob Storage backend.
|
||||||
func NewStorageBackend(opts Config, logFunc storage.Log) (storage.Backend, error) {
|
func NewStorageBackend(opts Config, logFunc storage.Log) (storage.Backend, error) {
|
||||||
storage := azureBlobStorage{}
|
cred, err := azblob.NewSharedKeyCredential(opts.AccountName, opts.PrimaryAccountKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("NewStorageBackend: error creating shared Azure credential: %w", err)
|
||||||
|
}
|
||||||
|
client, err := azblob.NewClientWithSharedKeyCredential(fmt.Sprintf(opts.Endpoint, opts.AccountName), cred, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("NewStorageBackend: error creating Azure client: %w", err)
|
||||||
|
}
|
||||||
|
storage := azureBlobStorage{
|
||||||
|
client: client,
|
||||||
|
containerName: opts.ContainerName,
|
||||||
|
}
|
||||||
return &storage, nil
|
return &storage, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the name of the storage backend
|
// Name returns the name of the storage backend
|
||||||
func (v *azureBlobStorage) Name() string {
|
func (b *azureBlobStorage) Name() string {
|
||||||
return "Azure"
|
return "Azure"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy copies the given file to the storage backend.
|
// Copy copies the given file to the storage backend.
|
||||||
func (b *azureBlobStorage) Copy(file string) error {
|
func (b *azureBlobStorage) Copy(file string) error {
|
||||||
|
fileReader, err := os.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("(*azureBlobStorage).Copy: error opening file %s: %w", file, err)
|
||||||
|
}
|
||||||
|
_, err = b.client.UploadStream(context.TODO(),
|
||||||
|
b.containerName,
|
||||||
|
file,
|
||||||
|
fileReader,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("(*azureBlobStorage).Copy: error uploading file %s: %w", file, err)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user