diff --git a/thirdparty/s3-datastore/datastore.go b/thirdparty/s3-datastore/datastore.go index 87e21d729..981ab5415 100644 --- a/thirdparty/s3-datastore/datastore.go +++ b/thirdparty/s3-datastore/datastore.go @@ -1,6 +1,7 @@ package s3datastore import ( + "encoding/hex" "errors" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/crowdmob/goamz/s3" @@ -19,25 +20,42 @@ type S3Datastore struct { Bucket string } +func (ds *S3Datastore) encode(key datastore.Key) string { + return hex.EncodeToString(key.Bytes()) +} + +func (ds *S3Datastore) decode(raw string) (datastore.Key, bool) { + k, err := hex.DecodeString(raw) + if err != nil { + return datastore.Key{}, false + } + return datastore.NewKey(string(k)), true +} + func (ds *S3Datastore) Put(key datastore.Key, value interface{}) (err error) { data, ok := value.([]byte) if !ok { return ErrInvalidType } // TODO extract perms and s3 options - return ds.Client.Bucket(ds.Bucket).Put(key.String(), data, "application/protobuf", s3.PublicRead, s3.Options{}) + + k := ds.encode(key) + return ds.Client.Bucket(ds.Bucket).Put(k, data, "application/protobuf", s3.PublicRead, s3.Options{}) } func (ds *S3Datastore) Get(key datastore.Key) (value interface{}, err error) { - return ds.Client.Bucket(ds.Bucket).Get(key.String()) + k := ds.encode(key) + return ds.Client.Bucket(ds.Bucket).Get(k) } func (ds *S3Datastore) Has(key datastore.Key) (exists bool, err error) { - return ds.Client.Bucket(ds.Bucket).Exists(key.String()) + k := ds.encode(key) + return ds.Client.Bucket(ds.Bucket).Exists(k) } func (ds *S3Datastore) Delete(key datastore.Key) (err error) { - return ds.Client.Bucket(ds.Bucket).Del(key.String()) + k := ds.encode(key) + return ds.Client.Bucket(ds.Bucket).Del(k) } func (ds *S3Datastore) Query(q query.Query) (query.Results, error) {