Skip to content

Commit fe0542e

Browse files
feat: S3: add logic to download and save locally the DB dump from S3
1 parent 936f7a3 commit fe0542e

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

pkg/dump/s3.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ func NewS3Uploader(localDump bool) (*S3Uploader, error) {
2323

2424
region := os.Getenv("AWS_REGION")
2525
if region == "" {
26-
return nil, fmt.Errorf("environment variable 'AWS_REGION' is not set")
26+
os.Setenv("AWS_REGION", "eu-south-1")
27+
bucket = "eu-south-1"
28+
fmt.Println("environment variable 'AWS_REGION' is not set. Set it to `eu-south-1`")
2729
}
2830

2931
return &S3Uploader{
@@ -35,6 +37,28 @@ func NewS3Uploader(localDump bool) (*S3Uploader, error) {
3537
}
3638
}
3739

40+
func NewS3Downloader(s3Dump bool) (*S3Uploader, error) {
41+
if s3Dump {
42+
region := os.Getenv("AWS_REGION")
43+
if region == "" {
44+
os.Setenv("AWS_REGION", "eu-south-1")
45+
region = "eu-south-1"
46+
fmt.Println("environment variable 'AWS_REGION' is not set. Set it to default 'eu-south-1'")
47+
return &S3Uploader{
48+
bucket: "",
49+
region: "eu-south-1",
50+
}, nil
51+
}
52+
53+
return &S3Uploader{
54+
bucket: "",
55+
region: region,
56+
}, nil
57+
} else {
58+
return &S3Uploader{}, nil
59+
}
60+
}
61+
3862
func (s *S3Uploader) Upload(localPath string, remotePath string) (string, error) {
3963
s3Uri := fmt.Sprintf("s3://%s/%s", s.bucket, remotePath)
4064
// We'll use AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from the environment
@@ -51,7 +75,7 @@ func (s *S3Uploader) Upload(localPath string, remotePath string) (string, error)
5175
}
5276

5377
func (s *S3Uploader) Download(remotePath string, localPath string) (string, error) {
54-
s3Uri := fmt.Sprintf("s3://%s/%s", s.bucket, remotePath)
78+
s3Uri := fmt.Sprintf("%s", remotePath)
5579
cmd := exec.Command("aws", "s3", "cp", s3Uri, localPath, "--region", s.region)
5680
var stderr bytes.Buffer
5781
cmd.Stderr = &stderr

0 commit comments

Comments
 (0)