From d61aadaa5badd47621b5157d8a9372a2a80726ff Mon Sep 17 00:00:00 2001 From: 3pointer Date: Thu, 31 Dec 2020 15:51:51 +0800 Subject: [PATCH] cherry pick #664 to release-4.0 Signed-off-by: ti-srebot --- 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)