From fe20ebca9765dd23e4e7e95aea7e6be9a47239c8 Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 30 Dec 2022 11:16:22 +0800 Subject: [PATCH] object/OVH: support OVH region (#3133) --- pkg/object/object_storage_test.go | 23 +++++++++++++++++++++-- pkg/object/s3.go | 13 +++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/pkg/object/object_storage_test.go b/pkg/object/object_storage_test.go index 80e07a6c0f12..de47b163f3fc 100644 --- a/pkg/object/object_storage_test.go +++ b/pkg/object/object_storage_test.go @@ -378,19 +378,38 @@ func TestS3(t *testing.T) { } func TestOracleCompileRegexp(t *testing.T) { - ep := "zhijian-test.axntujn0ebj1.compat.objectstorage.ap-singapore-1.oraclecloud.com" + ep := "axntujn0ebj1.compat.objectstorage.ap-singapore-1.oraclecloud.com" oracleCompile := regexp.MustCompile(oracleCompileRegexp) if oracleCompile.MatchString(ep) { - if submatch := oracleCompile.FindStringSubmatch(ep); len(submatch) == 2 { + if submatch := oracleCompile.FindStringSubmatch(ep); len(submatch) >= 2 { if submatch[1] != "ap-singapore-1" { t.Fatalf("oracle endpoint parse failed") } + } else { + t.Fatalf("oracle endpoint parse failed") } } else { t.Fatalf("oracle endpoint parse failed") } } +func TestOVHCompileRegexp(t *testing.T) { + for _, ep := range []string{"s3.gra.cloud.ovh.net", "s3.gra.perf.cloud.ovh.net", "s3.gra.io.cloud.ovh.net"} { + ovhCompile := regexp.MustCompile(OVHCompileRegexp) + if ovhCompile.MatchString(ep) { + if submatch := ovhCompile.FindStringSubmatch(ep); len(submatch) >= 2 { + if submatch[1] != "gra" { + t.Fatalf("ovh endpoint parse failed") + } + } else { + t.Fatalf("ovh endpoint parse failed") + } + } else { + t.Fatalf("ovh endpoint parse failed") + } + } +} + func TestOSS(t *testing.T) { if os.Getenv("ALICLOUD_ACCESS_KEY_ID") == "" { t.SkipNow() diff --git a/pkg/object/s3.go b/pkg/object/s3.go index 8d22131cb07a..8841f2575003 100644 --- a/pkg/object/s3.go +++ b/pkg/object/s3.go @@ -351,6 +351,7 @@ func parseRegion(endpoint string) string { } var oracleCompileRegexp = `.*\.compat.objectstorage\.(.*)\.oraclecloud\.com` +var OVHCompileRegexp = `^s3\.(\w*)(\.\w*)?\.cloud\.ovh\.net$` func newS3(endpoint, accessKey, secretKey, token string) (ObjectStorage, error) { if !strings.Contains(endpoint, "://") { @@ -419,10 +420,14 @@ func newS3(endpoint, accessKey, secretKey, token string) (ObjectStorage, error) // compatible s3 bucketName = hostParts[0] ep = hostParts[1] - oracleCompile := regexp.MustCompile(oracleCompileRegexp) - if oracleCompile.MatchString(ep) { - if submatch := oracleCompile.FindStringSubmatch(ep); len(submatch) == 2 { - region = submatch[1] + + for _, compileRegexp := range []string{oracleCompileRegexp, OVHCompileRegexp} { + compile := regexp.MustCompile(compileRegexp) + if compile.MatchString(ep) { + if submatch := compile.FindStringSubmatch(ep); len(submatch) >= 2 { + region = submatch[1] + break + } } } }