Skip to content

Commit

Permalink
feat: support for custom s3 storage
Browse files Browse the repository at this point in the history
  • Loading branch information
dexyk committed Nov 14, 2024
1 parent 6effb53 commit 219bf71
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/api/signed_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ func (u *SignedURL) GetObject() (string, error) {
return parseLocalhostURL(URL)

default:
if parsed, err := parseCustomS3URL(URL); err == nil {
log.Debugf("Parsing custom S3 URL: %s\n", u.URL)
return parsed, nil
}

log.Warnf("Failed to parse URL '%s' - unrecognized host '%s'\n", u.URL, host)
return "", fmt.Errorf("unrecognized host %s", host)
}
Expand Down Expand Up @@ -257,6 +262,19 @@ func parseS3URL(URL *url.URL) (string, error) {
return parsed[3], nil
}

// S3 Custom URLs can be in format:
// 'https://artifacts.<base-domain>/<bucket-name>/<project-id>/<path>'
func parseCustomS3URL(URL *url.URL) (string, error) {
re := regexp.MustCompile(`https:\/\/artifacts\.([^/]+)\/[^/]+\/[^/]+\/([^?]+)\?`)
parsed := re.FindStringSubmatch(URL.String())
if len(parsed) < 3 {
log.Warn("Failed to parse custom S3 URL.\n")
return "", fmt.Errorf("")
}

return parsed[2], nil
}

// Localhost URLs are used during tests
func parseLocalhostURL(URL *url.URL) (string, error) {
// we don't want the leading slash
Expand Down
7 changes: 7 additions & 0 deletions pkg/api/signed_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func Test__GetObject(t *testing.T) {
assert.Equal(t, "artifacts/project/projectid/mydir/myfile.txt", obj)
})

t.Run("Custom S3 - file", func(t *testing.T) {
signedURL := SignedURL{URL: "https://artifacts.test.s3-provider.com/art-bucket/projectid/artifacts/project/projectid/myfile.txt?X-Amz-Whatever"}
obj, err := signedURL.GetObject()
assert.Nil(t, err)
assert.Equal(t, "artifacts/project/projectid/myfile.txt", obj)
})

t.Run("bad host", func(t *testing.T) {
signedURL := SignedURL{URL: "https://somehost.com/projectid/artifacts/project/projectid/myfile.txt"}
_, err := signedURL.GetObject()
Expand Down

0 comments on commit 219bf71

Please sign in to comment.