mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-02 14:58:03 +08:00
thirdparty/s3-datastore: Datastore keys can be binary, hex encode them for S3
License: MIT Signed-off-by: Tommi Virtanen <tv@eagain.net>
This commit is contained in:
parent
9497e26fbd
commit
33d41285d6
26
thirdparty/s3-datastore/datastore.go
vendored
26
thirdparty/s3-datastore/datastore.go
vendored
@ -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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user