| 1 |
// Package s3 implements a koanf.Provider that takes a []byte slice |
| 2 |
// and provides it to koanf to be parsed by a koanf.Parser. |
| 3 |
package s3 |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"io" |
| 8 |
|
| 9 |
"github.com/rhnvrm/simples3" |
| 10 |
) |
| 11 |
|
| 12 |
// Config for the provider. |
| 13 |
type Config struct { |
| 14 |
// AWS Access Key |
| 15 |
AccessKey string |
| 16 |
|
| 17 |
// AWS Secret Key |
| 18 |
SecretKey string |
| 19 |
|
| 20 |
// AWS region |
| 21 |
Region string |
| 22 |
|
| 23 |
// Bucket Name |
| 24 |
Bucket string |
| 25 |
|
| 26 |
// Object Key |
| 27 |
ObjectKey string |
| 28 |
|
| 29 |
// Optional: Custom S3 compatible endpoint |
| 30 |
Endpoint string |
| 31 |
} |
| 32 |
|
| 33 |
// S3 implements a s3 provider. |
| 34 |
type S3 struct { |
| 35 |
s3 *simples3.S3 |
| 36 |
cfg Config |
| 37 |
} |
| 38 |
|
| 39 |
// Provider returns a provider that takes a simples3 config. |
| 40 |
func Provider(cfg Config) *S3 { |
| 41 |
s3 := simples3.New(cfg.Region, cfg.AccessKey, cfg.SecretKey) |
| 42 |
s3.SetEndpoint(cfg.Endpoint) |
| 43 |
|
| 44 |
return &S3{s3: s3, cfg: cfg} |
| 45 |
} |
| 46 |
|
| 47 |
// ReadBytes reads the contents of a file on s3 and returns the bytes. |
| 48 |
func (r *S3) ReadBytes() ([]byte, error) { |
| 49 |
resp, err := r.s3.FileDownload(simples3.DownloadInput{ |
| 50 |
Bucket: r.cfg.Bucket, |
| 51 |
ObjectKey: r.cfg.ObjectKey, |
| 52 |
}) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
|
| 57 |
defer resp.Close() |
| 58 |
|
| 59 |
data, err := io.ReadAll(resp) |
| 60 |
if err != nil { |
| 61 |
return nil, err |
| 62 |
} |
| 63 |
|
| 64 |
return data, nil |
| 65 |
} |
| 66 |
|
| 67 |
// Read is not supported for s3 provider. |
| 68 |
func (r *S3) Read() (map[string]any, error) { |
| 69 |
return nil, errors.New("s3 provider does not support this method") |
| 70 |
} |