From 8085ba4494de3ca5dbd5303053fecc13f3febf05 Mon Sep 17 00:00:00 2001 From: Krishnan Parthasarathi Date: Tue, 6 Jun 2017 06:13:53 +0000 Subject: [PATCH] Filter out internal object prefix during listing (#4435) We use ZZZZ-Minio/ prefix internally in our GCS gateway which should be filtered out in the response to ListObjects. --- cmd/gateway-gcs.go | 9 +++++++-- cmd/gateway-gcs_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/cmd/gateway-gcs.go b/cmd/gateway-gcs.go index ad785d92a..bcace2241 100644 --- a/cmd/gateway-gcs.go +++ b/cmd/gateway-gcs.go @@ -44,6 +44,11 @@ const ( ZZZZMinioPrefix = "ZZZZ-Minio" ) +// Check if object prefix is "ZZZZ_Minio". +func isGCSPrefix(prefix string) bool { + return strings.TrimSuffix(prefix, slashSeparator) == ZZZZMinioPrefix +} + // Convert Minio errors to minio object layer errors. func gcsToObjectError(err error, params ...string) error { if err == nil { @@ -325,7 +330,7 @@ func (l *gcsGateway) ListObjects(bucket string, prefix string, marker string, de attrs, _ := it.Next() if attrs == nil { - } else if attrs.Prefix == ZZZZMinioPrefix { + } else if isGCSPrefix(attrs.Prefix) { break } @@ -342,7 +347,7 @@ func (l *gcsGateway) ListObjects(bucket string, prefix string, marker string, de nextMarker = toGCSPageToken(attrs.Name) - if attrs.Prefix == ZZZZMinioPrefix { + if isGCSPrefix(attrs.Prefix) { // we don't return our metadata prefix continue } else if attrs.Prefix != "" { diff --git a/cmd/gateway-gcs_test.go b/cmd/gateway-gcs_test.go index 0466a8d65..1b3dab9a3 100644 --- a/cmd/gateway-gcs_test.go +++ b/cmd/gateway-gcs_test.go @@ -129,3 +129,38 @@ func TestValidGCSProjectID(t *testing.T) { } } } + +// Test for isGCSPrefix +func TestIsGCSPrefix(t *testing.T) { + testCases := []struct { + prefix string + expectedRes bool + }{ + // Regular prefix without a trailing slash + { + prefix: "hello", + expectedRes: false, + }, + // Regular prefix with a trailing slash + { + prefix: "hello/", + expectedRes: false, + }, + // GCS prefix without a trailing slash + { + prefix: ZZZZMinioPrefix, + expectedRes: true, + }, + // GCS prefix with a trailing slash + { + prefix: ZZZZMinioPrefix + "/", + expectedRes: true, + }, + } + + for i, tc := range testCases { + if actualRes := isGCSPrefix(tc.prefix); actualRes != tc.expectedRes { + t.Errorf("%d: Expected isGCSPrefix to return %v but got %v", i, tc.expectedRes, actualRes) + } + } +}