diff --git a/pkg/storage/chunk/client/aws/s3_storage_client.go b/pkg/storage/chunk/client/aws/s3_storage_client.go index 26c2807e120e7..df20761489afd 100644 --- a/pkg/storage/chunk/client/aws/s3_storage_client.go +++ b/pkg/storage/chunk/client/aws/s3_storage_client.go @@ -338,7 +338,7 @@ func (a *S3ObjectClient) ObjectExistsWithSize(ctx context.Context, objectKey str return nil }) if lastErr == nil { - return true, 0, nil + return true, objectSize, nil } if a.IsObjectNotFoundErr(lastErr) { @@ -348,11 +348,7 @@ func (a *S3ObjectClient) ObjectExistsWithSize(ctx context.Context, objectKey str retries.Wait() } - if lastErr != nil { - return false, 0, lastErr - } - - return true, objectSize, nil + return false, 0, lastErr } // DeleteObject deletes the specified objectKey from the appropriate S3 bucket diff --git a/pkg/storage/chunk/client/aws/s3_storage_client_test.go b/pkg/storage/chunk/client/aws/s3_storage_client_test.go index e18160a3fa003..77ec4422d7366 100644 --- a/pkg/storage/chunk/client/aws/s3_storage_client_test.go +++ b/pkg/storage/chunk/client/aws/s3_storage_client_test.go @@ -309,6 +309,38 @@ func (m *MockS3Client) HeadObject(input *s3.HeadObjectInput) (*s3.HeadObjectOutp return m.HeadObjectFunc(input) } +func Test_ExistsWithSize(t *testing.T) { + mockS3 := &MockS3Client{ + HeadObjectFunc: func(_ *s3.HeadObjectInput) (*s3.HeadObjectOutput, error) { + var size int64 + size = 128 + return &s3.HeadObjectOutput{ContentLength: &size}, nil + }, + } + + c, err := NewS3ObjectClient(S3Config{ + AccessKeyID: "foo", + SecretAccessKey: flagext.SecretWithValue("bar"), + BackoffConfig: backoff.Config{MaxRetries: 3}, + BucketNames: "foo", + Inject: func(_ http.RoundTripper) http.RoundTripper { + return RoundTripperFunc(func(_ *http.Request) (*http.Response, error) { + return &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(bytes.NewReader([]byte("object content"))), + }, nil + }) + }, + }, hedging.Config{}) + require.NoError(t, err) + c.S3 = mockS3 + + exists, size, err := c.ObjectExistsWithSize(context.Background(), "abc") + require.NoError(t, err) + require.EqualValues(t, 128, size) + require.True(t, exists) +} + func Test_RetryLogic(t *testing.T) { for _, tc := range []struct { name string