github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/storage/bucket/prefixed_bucket_client.go (about)

     1  package bucket
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"strings"
     7  
     8  	"github.com/thanos-io/thanos/pkg/objstore"
     9  )
    10  
    11  type PrefixedBucketClient struct {
    12  	bucket objstore.Bucket
    13  	prefix string
    14  }
    15  
    16  // NewPrefixedBucketClient returns a new PrefixedBucketClient.
    17  func NewPrefixedBucketClient(bucket objstore.Bucket, prefix string) *PrefixedBucketClient {
    18  	return &PrefixedBucketClient{
    19  		bucket: bucket,
    20  		prefix: prefix,
    21  	}
    22  }
    23  
    24  func (b *PrefixedBucketClient) fullName(name string) string {
    25  	return b.prefix + objstore.DirDelim + name
    26  }
    27  
    28  // Close implements io.Closer
    29  func (b *PrefixedBucketClient) Close() error {
    30  	return b.bucket.Close()
    31  }
    32  
    33  // Upload the contents of the reader as an object into the bucket.
    34  func (b *PrefixedBucketClient) Upload(ctx context.Context, name string, r io.Reader) (err error) {
    35  	err = b.bucket.Upload(ctx, b.fullName(name), r)
    36  	return
    37  }
    38  
    39  // Delete removes the object with the given name.
    40  func (b *PrefixedBucketClient) Delete(ctx context.Context, name string) error {
    41  	return b.bucket.Delete(ctx, b.fullName(name))
    42  }
    43  
    44  // Name returns the bucket name for the provider.
    45  func (b *PrefixedBucketClient) Name() string { return b.bucket.Name() }
    46  
    47  // Iter calls f for each entry in the given directory (not recursive.). The argument to f is the full
    48  // object name including the prefix of the inspected directory. The configured prefix will be stripped
    49  // before supplied function is applied.
    50  func (b *PrefixedBucketClient) Iter(ctx context.Context, dir string, f func(string) error, options ...objstore.IterOption) error {
    51  	return b.bucket.Iter(ctx, b.fullName(dir), func(s string) error {
    52  		return f(strings.TrimPrefix(s, b.prefix+objstore.DirDelim))
    53  	}, options...)
    54  }
    55  
    56  // Get returns a reader for the given object name.
    57  func (b *PrefixedBucketClient) Get(ctx context.Context, name string) (io.ReadCloser, error) {
    58  	return b.bucket.Get(ctx, b.fullName(name))
    59  }
    60  
    61  // GetRange returns a new range reader for the given object name and range.
    62  func (b *PrefixedBucketClient) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) {
    63  	return b.bucket.GetRange(ctx, b.fullName(name), off, length)
    64  }
    65  
    66  // Exists checks if the given object exists in the bucket.
    67  func (b *PrefixedBucketClient) Exists(ctx context.Context, name string) (bool, error) {
    68  	return b.bucket.Exists(ctx, b.fullName(name))
    69  }
    70  
    71  // IsObjNotFoundErr returns true if error means that object is not found. Relevant to Get operations.
    72  func (b *PrefixedBucketClient) IsObjNotFoundErr(err error) bool {
    73  	return b.bucket.IsObjNotFoundErr(err)
    74  }
    75  
    76  // Attributes returns attributes of the specified object.
    77  func (b *PrefixedBucketClient) Attributes(ctx context.Context, name string) (objstore.ObjectAttributes, error) {
    78  	return b.bucket.Attributes(ctx, b.fullName(name))
    79  }
    80  
    81  // WithExpectedErrs allows to specify a filter that marks certain errors as expected, so it will not increment
    82  // thanos_objstore_bucket_operation_failures_total metric.
    83  func (b *PrefixedBucketClient) ReaderWithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.BucketReader {
    84  	return b.WithExpectedErrs(fn)
    85  }
    86  
    87  // ReaderWithExpectedErrs allows to specify a filter that marks certain errors as expected, so it will not increment
    88  // thanos_objstore_bucket_operation_failures_total metric.
    89  func (b *PrefixedBucketClient) WithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.Bucket {
    90  	if ib, ok := b.bucket.(objstore.InstrumentedBucket); ok {
    91  		return &PrefixedBucketClient{
    92  			bucket: ib.WithExpectedErrs(fn),
    93  			prefix: b.prefix,
    94  		}
    95  	}
    96  	return b
    97  }