@cryptotaxi247 / kubo / commits / 33d41285d

thirdparty/s3-datastore: Datastore keys can be binary, hex encode them for S3

License: MIT Signed-off-by: Tommi Virtanen <tv@eagain.net>

Tommi Virtanen committed May 20, 2015 at 14:17 UTC 33d41285d6ff6fb105932d705680ac0fd2a6bb2a
1 file changed +22 -4
thirdparty/s3-datastore/datastore.go
+22 -4
@@ -1,6 +1,7 @@
1 package s3datastore
2
3 import (
4 + "encoding/hex"
5 "errors"
6
7 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/crowdmob/goamz/s3"
@@ -19,25 +20,42 @@ type S3Datastore struct {
20 Bucket string
21 }
22
23 +func (ds *S3Datastore) encode(key datastore.Key) string {
24 + return hex.EncodeToString(key.Bytes())
25 +}
26 +
27 +func (ds *S3Datastore) decode(raw string) (datastore.Key, bool) {
28 + k, err := hex.DecodeString(raw)
29 + if err != nil {
30 + return datastore.Key{}, false
31 + }
32 + return datastore.NewKey(string(k)), true
33 +}
34 +
35 func (ds *S3Datastore) Put(key datastore.Key, value interface{}) (err error) {
36 data, ok := value.([]byte)
37 if !ok {
38 return ErrInvalidType
39 }
40 // TODO extract perms and s3 options
28 - return ds.Client.Bucket(ds.Bucket).Put(key.String(), data, "application/protobuf", s3.PublicRead, s3.Options{})
41 +
42 + k := ds.encode(key)
43 + return ds.Client.Bucket(ds.Bucket).Put(k, data, "application/protobuf", s3.PublicRead, s3.Options{})
44 }
45
46 func (ds *S3Datastore) Get(key datastore.Key) (value interface{}, err error) {
32 - return ds.Client.Bucket(ds.Bucket).Get(key.String())
47 + k := ds.encode(key)
48 + return ds.Client.Bucket(ds.Bucket).Get(k)
49 }
50
51 func (ds *S3Datastore) Has(key datastore.Key) (exists bool, err error) {
36 - return ds.Client.Bucket(ds.Bucket).Exists(key.String())
52 + k := ds.encode(key)
53 + return ds.Client.Bucket(ds.Bucket).Exists(k)
54 }
55
56 func (ds *S3Datastore) Delete(key datastore.Key) (err error) {
40 - return ds.Client.Bucket(ds.Bucket).Del(key.String())
57 + k := ds.encode(key)
58 + return ds.Client.Bucket(ds.Bucket).Del(k)
59 }
60
61 func (ds *S3Datastore) Query(q query.Query) (query.Results, error) {