-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dump): Add support for dumps to an S3 bucket
- Loading branch information
Showing
7 changed files
with
239 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package s3 | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
"k8s.io/utils/ptr" | ||
) | ||
|
||
const Schema = "s3://" | ||
|
||
func IsS3(path string) bool { | ||
return strings.HasPrefix(path, Schema) | ||
} | ||
|
||
func IsS3Dir(path string) bool { | ||
if !IsS3(path) { | ||
return false | ||
} | ||
if strings.HasSuffix(path, "/") { | ||
return true | ||
} | ||
trimmed := strings.TrimPrefix(path, Schema) | ||
return !strings.Contains(trimmed, "/") | ||
} | ||
|
||
func CreateUpload(ctx context.Context, r io.ReadCloser, key string) error { | ||
defer func(r io.ReadCloser) { | ||
_ = r.Close() | ||
}(r) | ||
|
||
sdkConfig, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
u, err := url.Parse(key) | ||
if err != nil { | ||
return err | ||
} | ||
u.Path = strings.TrimLeft(u.Path, "/") | ||
|
||
s3Client := s3.NewFromConfig(sdkConfig) | ||
_, err = s3Client.PutObject(ctx, &s3.PutObjectInput{ | ||
Bucket: ptr.To(u.Host), | ||
Key: ptr.To(u.Path), | ||
Body: r, | ||
}) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package s3 | ||
|
||
import "testing" | ||
|
||
func TestIsS3(t *testing.T) { | ||
type args struct { | ||
path string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{"relative local", args{"test.sql"}, false}, | ||
{"absolute local", args{"/home/test/test.sql"}, false}, | ||
{"s3 bucket", args{"s3://test"}, true}, | ||
{"s3 bucket file", args{"s3://test/test.sql"}, true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := IsS3(tt.args.path); got != tt.want { | ||
t.Errorf("IsS3() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsS3Dir(t *testing.T) { | ||
type args struct { | ||
path string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{"relative local", args{"test.sql"}, false}, | ||
{"absolute local", args{"/home/test/test.sql"}, false}, | ||
{"s3 bucket", args{"s3://test"}, true}, | ||
{"s3 bucket file", args{"s3://test/test.sql"}, false}, | ||
{"s3 bucket dir", args{"s3://test/subdir/"}, true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := IsS3Dir(tt.args.path); got != tt.want { | ||
t.Errorf("IsS3Dir() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |