From 707abab039314501829c2ba59595821a27f14383 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Fri, 8 Jan 2021 16:30:07 +0800 Subject: [PATCH] cherry pick #664 to release-4.0 (#673) Signed-off-by: ti-srebot Co-authored-by: 3pointer --- pkg/storage/parse.go | 5 ++++- pkg/storage/parse_test.go | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/storage/parse.go b/pkg/storage/parse.go index 36487cc54..42a50cdf4 100644 --- a/pkg/storage/parse.go +++ b/pkg/storage/parse.go @@ -70,7 +70,10 @@ func ParseBackend(rawURL string, options *BackendOptions) (*backup.StorageBacken return &backup.StorageBackend{Backend: &backup.StorageBackend_S3{S3: s3}}, nil case "gs", "gcs": - prefix := strings.Trim(u.Path[1:], "/") + if u.Host == "" { + return nil, errors.Annotatef(berrors.ErrStorageInvalidConfig, "please specify the bucket for gcs in %s", rawURL) + } + prefix := strings.Trim(u.Path, "/") gcs := &backup.GCS{Bucket: u.Host, Prefix: prefix} if options == nil { options = &BackendOptions{} diff --git a/pkg/storage/parse_test.go b/pkg/storage/parse_test.go index 4b2072f0c..cd4312081 100644 --- a/pkg/storage/parse_test.go +++ b/pkg/storage/parse_test.go @@ -91,6 +91,15 @@ func (r *testStorageSuite) TestCreateStorage(c *C) { c.Assert(gcs.Endpoint, Equals, "https://gcs.example.com/") c.Assert(gcs.CredentialsBlob, Equals, "") + s, err = ParseBackend("gcs://bucket2", gcsOpt) + c.Assert(err, IsNil) + gcs = s.GetGcs() + c.Assert(gcs, NotNil) + c.Assert(gcs.Bucket, Equals, "bucket2") + c.Assert(gcs.Prefix, Equals, "") + c.Assert(gcs.Endpoint, Equals, "https://gcs.example.com/") + c.Assert(gcs.CredentialsBlob, Equals, "") + var credFeilPerm os.FileMode = 0o600 fakeCredentialsFile := filepath.Join(c.MkDir(), "fakeCredentialsFile") err = ioutil.WriteFile(fakeCredentialsFile, []byte("fakeCredentials"), credFeilPerm)