1
0
Fork 0

Add support for custom S3 endpoint

master
Philip O'Toole 1 year ago
parent 9697eb83b4
commit ea23906180

@ -14,15 +14,17 @@ import (
// S3Config is the subconfig for the S3 storage type // S3Config is the subconfig for the S3 storage type
type S3Config struct { type S3Config struct {
Endpoint string `json:"endpoint,omitempty"`
Region string `json:"region"`
AccessKeyID string `json:"access_key_id"` AccessKeyID string `json:"access_key_id"`
SecretAccessKey string `json:"secret_access_key"` SecretAccessKey string `json:"secret_access_key"`
Region string `json:"region"`
Bucket string `json:"bucket"` Bucket string `json:"bucket"`
Path string `json:"path"` Path string `json:"path"`
} }
// S3Client is a client for uploading data to S3. // S3Client is a client for uploading data to S3.
type S3Client struct { type S3Client struct {
endpoint string
region string region string
accessKey string accessKey string
secretKey string secretKey string
@ -35,8 +37,9 @@ type S3Client struct {
} }
// NewS3Client returns an instance of an S3Client. // NewS3Client returns an instance of an S3Client.
func NewS3Client(region, accessKey, secretKey, bucket, key string) *S3Client { func NewS3Client(endpoint, region, accessKey, secretKey, bucket, key string) *S3Client {
return &S3Client{ return &S3Client{
endpoint: endpoint,
region: region, region: region,
accessKey: accessKey, accessKey: accessKey,
secretKey: secretKey, secretKey: secretKey,
@ -105,6 +108,7 @@ func (s *S3Client) Download(ctx context.Context, writer io.WriterAt) error {
func (s *S3Client) createSession() (*session.Session, error) { func (s *S3Client) createSession() (*session.Session, error) {
sess, err := session.NewSession(&aws.Config{ sess, err := session.NewSession(&aws.Config{
Endpoint: aws.String(s.endpoint),
Region: aws.String(s.region), Region: aws.String(s.region),
Credentials: credentials.NewStaticCredentials(s.accessKey, s.secretKey, ""), Credentials: credentials.NewStaticCredentials(s.accessKey, s.secretKey, ""),
}) })

@ -14,7 +14,7 @@ import (
) )
func Test_NewS3Client(t *testing.T) { func Test_NewS3Client(t *testing.T) {
c := NewS3Client("region1", "access", "secret", "bucket2", "key3") c := NewS3Client("endpoint1", "region1", "access", "secret", "bucket2", "key3")
if c.region != "region1" { if c.region != "region1" {
t.Fatalf("expected region to be %q, got %q", "region1", c.region) t.Fatalf("expected region to be %q, got %q", "region1", c.region)
} }
@ -33,13 +33,14 @@ func Test_NewS3Client(t *testing.T) {
} }
func Test_S3Client_String(t *testing.T) { func Test_S3Client_String(t *testing.T) {
c := NewS3Client("region1", "access", "secret", "bucket2", "key3") c := NewS3Client("endpoint1", "region1", "access", "secret", "bucket2", "key3")
if c.String() != "s3://bucket2/key3" { if c.String() != "s3://bucket2/key3" {
t.Fatalf("expected String() to be %q, got %q", "s3://bucket2/key3", c.String()) t.Fatalf("expected String() to be %q, got %q", "s3://bucket2/key3", c.String())
} }
} }
func TestS3ClientUploadOK(t *testing.T) { func TestS3ClientUploadOK(t *testing.T) {
endpoint := "https://my-custom-s3-endpoint.com"
region := "us-west-2" region := "us-west-2"
accessKey := "your-access-key" accessKey := "your-access-key"
secretKey := "your-secret-key" secretKey := "your-secret-key"
@ -68,6 +69,7 @@ func TestS3ClientUploadOK(t *testing.T) {
} }
client := &S3Client{ client := &S3Client{
endpoint: endpoint,
region: region, region: region,
accessKey: accessKey, accessKey: accessKey,
secretKey: secretKey, secretKey: secretKey,
@ -162,6 +164,7 @@ func TestS3ClientDownloadOK(t *testing.T) {
} }
func TestS3ClientDownloadFail(t *testing.T) { func TestS3ClientDownloadFail(t *testing.T) {
endpoint := "https://my-custom-s3-endpoint.com"
region := "us-west-2" region := "us-west-2"
accessKey := "your-access-key" accessKey := "your-access-key"
secretKey := "your-secret-key" secretKey := "your-secret-key"
@ -175,6 +178,7 @@ func TestS3ClientDownloadFail(t *testing.T) {
} }
client := &S3Client{ client := &S3Client{
endpoint: endpoint,
region: region, region: region,
accessKey: accessKey, accessKey: accessKey,
secretKey: secretKey, secretKey: secretKey,

@ -220,7 +220,8 @@ func startAutoBackups(ctx context.Context, cfg *Config, str *store.Store) (*uplo
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse auto-backup file: %s", err.Error()) return nil, fmt.Errorf("failed to parse auto-backup file: %s", err.Error())
} }
sc := aws.NewS3Client(s3cfg.Region, s3cfg.AccessKeyID, s3cfg.SecretAccessKey, s3cfg.Bucket, s3cfg.Path) sc := aws.NewS3Client(s3cfg.Endpoint, s3cfg.Region, s3cfg.AccessKeyID, s3cfg.SecretAccessKey,
s3cfg.Bucket, s3cfg.Path)
u := upload.NewUploader(sc, str, time.Duration(uCfg.Interval), !uCfg.NoCompress) u := upload.NewUploader(sc, str, time.Duration(uCfg.Interval), !uCfg.NoCompress)
go u.Start(ctx, nil) go u.Start(ctx, nil)
return u, nil return u, nil
@ -246,7 +247,8 @@ func downloadRestoreFile(ctx context.Context, cfgPath string) (path string, err
if err != nil { if err != nil {
return "", fmt.Errorf("failed to parse auto-restore file: %s", err.Error()) return "", fmt.Errorf("failed to parse auto-restore file: %s", err.Error())
} }
sc := aws.NewS3Client(s3cfg.Region, s3cfg.AccessKeyID, s3cfg.SecretAccessKey, s3cfg.Bucket, s3cfg.Path) sc := aws.NewS3Client(s3cfg.Endpoint, s3cfg.Region, s3cfg.AccessKeyID, s3cfg.SecretAccessKey,
s3cfg.Bucket, s3cfg.Path)
d := download.NewDownloader(sc) d := download.NewDownloader(sc)
// Create a temporary file to download to. // Create a temporary file to download to.

Loading…
Cancel
Save