· 7 years ago · Nov 14, 2018, 06:50 AM
1package s3ds
2
3import (
4 "fmt"
5
6 "github.com/ipfs/go-ipfs/plugin"
7 "github.com/ipfs/go-ipfs/repo"
8 "github.com/ipfs/go-ipfs/repo/fsrepo"
9
10 s3ds "github.com/joeltg/go-ds-s3"
11)
12
13// Plugins is exported list of plugins that will be loaded
14var Plugins = []plugin.Plugin{
15 &s3Plugin{},
16}
17
18type s3Plugin struct{}
19
20func (s3p s3Plugin) Name() string {
21 return "ds-s3"
22}
23
24func (s3p s3Plugin) Version() string {
25 return "0.0.1"
26}
27
28func (s3p s3Plugin) Init() error {
29 return nil
30}
31
32func (s3p s3Plugin) DatastoreTypeName() string {
33 return "s3ds"
34}
35
36func (s3p s3Plugin) DatastoreConfigParser() fsrepo.ConfigFromMap {
37 return func(m map[string]interface{}) (fsrepo.DatastoreConfig, error) {
38 accessKey, ok := m["accessKey"].(string)
39 if !ok {
40 return nil, fmt.Errorf("s3ds: no accessKey specified")
41 }
42
43 secretKey, ok := m["secretKey"].(string)
44 if !ok {
45 return nil, fmt.Errorf("s3ds: no secretKey specified")
46 }
47
48 sessionToken, ok := m["sessionToken"].(string)
49 if !ok {
50 return nil, fmt.Errorf("s3ds: no sessionToken specified")
51 }
52
53 bucket, ok := m["bucket"].(string)
54 if !ok {
55 return nil, fmt.Errorf("s3ds: no bucket specified")
56 }
57
58 region, ok := m["region"].(string)
59 if !ok {
60 return nil, fmt.Errorf("s3ds: no region specified")
61 }
62
63 regionEndpoint, ok := m["regionEndpoint"].(string)
64 if !ok {
65 return nil, fmt.Errorf("s3ds: no regionEndpoint specified")
66 }
67
68 rootDirectory, ok := m["rootDirectory"].(string)
69 if !ok {
70 return nil, fmt.Errorf("s3ds: no rootDirectory specified")
71 }
72
73 workers, ok := m["workers"].(float64)
74 if !ok {
75 return nil, fmt.Errorf("s3ds: no workers specified")
76 }
77
78 return &datastoreConfig{
79 cfg: &s3ds.Config{
80 AccessKey: accessKey,
81 SecretKey: secretKey,
82 SessionToken: sessionToken,
83 Bucket: bucket,
84 Region: region,
85 RegionEndpoint: regionEndpoint,
86 RootDirectory: rootDirectory,
87 Workers: int(workers),
88 },
89 }, nil
90 }
91}
92
93type datastoreConfig struct {
94 cfg *s3ds.Config
95}
96
97func (c *datastoreConfig) DiskSpec() fsrepo.DiskSpec {
98 return fsrepo.DiskSpec{
99 "bucket": c.cfg.Bucket,
100 "region": c.cfg.Region,
101 "rootDirectory": c.cfg.RootDirectory,
102 }
103}
104
105func (c *datastoreConfig) Create(path string) (repo.Datastore, error) {
106 return s3ds.NewS3Datastore(*c.cfg)
107}