2
0

prefixedbucketstorage.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package storage
  2. // Partially forked from Thanos prefixed bucket for prefix handling
  3. // Licensed under the Apache License 2.0
  4. // https://github.com/thanos-io/objstore/blob/main/prefixed_bucket.go
  5. import (
  6. "strings"
  7. "github.com/pkg/errors"
  8. )
  9. type PrefixedBucketStorage struct {
  10. storage Storage
  11. prefix string
  12. }
  13. func NewPrefixedBucketStorage(storage Storage, prefix string) (Storage, error) {
  14. if validPrefix(prefix) {
  15. return &PrefixedBucketStorage{
  16. storage: storage,
  17. prefix: strings.Trim(prefix, DirDelim),
  18. }, nil
  19. }
  20. return storage, errors.Errorf("failed to create prefixed storage: invalid prefix '%s'", prefix)
  21. }
  22. func validPrefix(prefix string) bool {
  23. prefix = strings.Replace(prefix, "/", "", -1)
  24. return len(prefix) > 0
  25. }
  26. func conditionalPrefix(prefix, name string) string {
  27. if len(name) > 0 && !strings.HasPrefix(name, prefix) {
  28. return withPrefix(prefix, name)
  29. }
  30. return name
  31. }
  32. func withPrefix(prefix, name string) string {
  33. return prefix + DirDelim + name
  34. }
  35. func (pbs *PrefixedBucketStorage) StorageType() StorageType {
  36. return pbs.storage.StorageType()
  37. }
  38. // FullPath returns the storage working path combined with the path provided.
  39. func (pbs *PrefixedBucketStorage) FullPath(name string) string {
  40. return pbs.storage.FullPath(conditionalPrefix(pbs.prefix, name))
  41. }
  42. // Exists checks if the given object exists.
  43. func (pbs *PrefixedBucketStorage) Exists(name string) (bool, error) {
  44. return pbs.storage.Exists(conditionalPrefix(pbs.prefix, name))
  45. }
  46. // List returns storage information for files on the provided path.
  47. func (pbs *PrefixedBucketStorage) List(path string) ([]*StorageInfo, error) {
  48. return pbs.storage.List(conditionalPrefix(pbs.prefix, path))
  49. }
  50. // ListDirectories returns storage information for only directories on the provided path.
  51. func (pbs *PrefixedBucketStorage) ListDirectories(path string) ([]*StorageInfo, error) {
  52. return pbs.storage.ListDirectories(conditionalPrefix(pbs.prefix, path))
  53. }
  54. // Read returns a reader for the given object name.
  55. func (pbs *PrefixedBucketStorage) Read(name string) ([]byte, error) {
  56. return pbs.storage.Read(conditionalPrefix(pbs.prefix, name))
  57. }
  58. // Remove deletes the object with the given name.
  59. func (pbs *PrefixedBucketStorage) Remove(name string) error {
  60. return pbs.storage.Remove(conditionalPrefix(pbs.prefix, name))
  61. }
  62. // Stat returns information about the specified object.
  63. func (pbs *PrefixedBucketStorage) Stat(name string) (*StorageInfo, error) {
  64. return pbs.storage.Stat(conditionalPrefix(pbs.prefix, name))
  65. }
  66. // Write uploads the contents of the reader as an object into the bucket.
  67. func (pbs *PrefixedBucketStorage) Write(name string, data []byte) error {
  68. return pbs.storage.Write(conditionalPrefix(pbs.prefix, name), data)
  69. }