bucketstorage.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package storage
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/pkg/errors"
  6. "gopkg.in/yaml.v2"
  7. )
  8. // StorageProvider is the type of provider used for storage if not leveraging a file implementation.
  9. type StorageProvider string
  10. const (
  11. S3 StorageProvider = "S3"
  12. // AZURE StorageProvider = "AZURE"
  13. // GCS StorageProvider = "GCS"
  14. )
  15. // StorageConfig is the configuration type used to
  16. type StorageConfig struct {
  17. Type StorageProvider `yaml:"type"`
  18. Config interface{} `yaml:"config"`
  19. }
  20. // NewBucketStorage initializes and returns new Storage implementation leveraging the storage provider
  21. // configuration. This configuration type uses the layout provided in thanos: https://thanos.io/tip/thanos/storage.md/
  22. func NewBucketStorage(config []byte) (Storage, error) {
  23. storageConfig := &StorageConfig{}
  24. if err := yaml.UnmarshalStrict(config, storageConfig); err != nil {
  25. return nil, errors.Wrap(err, "parsing config YAML file")
  26. }
  27. config, err := yaml.Marshal(storageConfig.Config)
  28. if err != nil {
  29. return nil, errors.Wrap(err, "marshal content of storage configuration")
  30. }
  31. var storage Storage
  32. switch strings.ToUpper(string(storageConfig.Type)) {
  33. case string(S3):
  34. storage, err = NewS3Storage(config)
  35. //case string(GCS):
  36. // storage, err = NewGCSStorage(config)
  37. //case string(AZURE):
  38. // storage, err = NewAzureStorage(config)
  39. default:
  40. return nil, errors.Errorf("storage with type %s is not supported", storageConfig.Type)
  41. }
  42. if err != nil {
  43. return nil, errors.Wrap(err, fmt.Sprintf("create %s client", storageConfig.Type))
  44. }
  45. return storage, nil
  46. }