feat: implement Azure Blob Storage client and refactor configuration structure
This commit is contained in:
68
internal/app/azure/main.go
Normal file
68
internal/app/azure/main.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package azure
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidConnectionString = errors.New("invalid connection string")
|
||||
ErrContainerNotFound = errors.New("container not found")
|
||||
ErrBlobNotFound = errors.New("blob not found")
|
||||
ErrInvalidInput = errors.New("invalid input parameters")
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
client *azblob.Client
|
||||
azureStorageConfig config.AzureStorageConfig
|
||||
}
|
||||
|
||||
func NewClient(azureStorageConfig config.AzureStorageConfig) (*Client, error) {
|
||||
protocol := "https"
|
||||
if !azureStorageConfig.UseHTTPS {
|
||||
protocol = "http"
|
||||
}
|
||||
|
||||
blobEndpoint, _ := url.JoinPath(azureStorageConfig.ServiceURL, azureStorageConfig.AccountName)
|
||||
connStr := fmt.Sprintf("DefaultEndpointsProtocol=%s;AccountName=%s;AccountKey=%s;BlobEndpoint=%s;",
|
||||
protocol, azureStorageConfig.AccountName, azureStorageConfig.AccountKey, blobEndpoint)
|
||||
|
||||
client, err := azblob.NewClientFromConnectionString(connStr, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating azure storage client: %w", err)
|
||||
}
|
||||
|
||||
return &Client{
|
||||
client: client,
|
||||
azureStorageConfig: azureStorageConfig,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateContainer(ctx context.Context, containerName string) error {
|
||||
if containerName == "" {
|
||||
return ErrInvalidInput
|
||||
}
|
||||
|
||||
_, err := c.client.CreateContainer(ctx, containerName, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating container %s: %w", containerName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) UploadBuffer(ctx context.Context, containerName, blobPath string, buffer []byte) error {
|
||||
if containerName == "" || blobPath == "" || buffer == nil {
|
||||
return ErrInvalidInput
|
||||
}
|
||||
|
||||
_, err := c.client.UploadBuffer(ctx, containerName, blobPath, buffer, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("uploading blob %s: %w", blobPath, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type AzureConfig struct {
|
||||
type AzureStorageConfig struct {
|
||||
AccountName string `env:"AZ_ACCOUNT_NAME"`
|
||||
Container string `env:"AZ_CONTAINER"`
|
||||
AccountKey string `env:"AZ_ACCOUNT_KEY"`
|
||||
@@ -17,10 +17,10 @@ type AzureConfig struct {
|
||||
}
|
||||
|
||||
type appConfig struct {
|
||||
SourceDbUrl string `env:"SOURCE_DB_URL,required"`
|
||||
TargetDbUrl string `env:"TARGET_DB_URL,required"`
|
||||
LogLevel string `env:"LOG_LEVEL" envDefault:"INFO"`
|
||||
Azure AzureConfig
|
||||
SourceDbUrl string `env:"SOURCE_DB_URL,required"`
|
||||
TargetDbUrl string `env:"TARGET_DB_URL,required"`
|
||||
LogLevel string `env:"LOG_LEVEL" envDefault:"INFO"`
|
||||
AzureStorage AzureStorageConfig
|
||||
}
|
||||
|
||||
func getAppConfig() appConfig {
|
||||
|
||||
Reference in New Issue
Block a user