storj/examples/pointerdb-client/main.go
JT Olio 9aee5efd99
pointerdb: separate client and server packages (#261)
* pointerdb: separate client and server packages

the reason for this is so that things the server needs (bolt, auth)
don't get imported if all you need is the client. will result in
smaller binaries and less flag definitions

* review comments
2018-08-22 09:07:00 -06:00

108 lines
2.5 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
p "storj.io/storj/pkg/paths"
client "storj.io/storj/pkg/pointerdb/pdbclient"
"storj.io/storj/pkg/storage/meta"
proto "storj.io/storj/protos/pointerdb"
)
var (
pointerdbClientPort string
)
func initializeFlags() {
flag.StringVar(&pointerdbClientPort, "pointerdbPort", ":8080", "this is your port")
flag.Parse()
}
func main() {
initializeFlags()
logger, _ := zap.NewDevelopment()
defer logger.Sync()
APIKey := []byte("abc123")
pdbclient, err := client.NewClient(pointerdbClientPort, APIKey)
if err != nil {
logger.Error("Failed to dial: ", zap.Error(err))
os.Exit(1)
}
logger.Debug(fmt.Sprintf("client dialed port %s", pointerdbClientPort))
ctx := context.Background()
// Example parameters to pass into API calls
var path = p.New("fold1/fold2/fold3/file.txt")
pointer := &proto.Pointer{
Type: proto.Pointer_INLINE,
InlineSegment: []byte("popcorn"),
}
// Example Put1
err = pdbclient.Put(ctx, path, pointer)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("couldn't put pointer in db", zap.Error(err))
} else {
logger.Debug("Success: put pointer in db")
}
// Example Put2
err = pdbclient.Put(ctx, p.New("fold1/fold2"), pointer)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("couldn't put pointer in db", zap.Error(err))
} else {
logger.Debug("Success: put pointer in db")
}
// Example Get
getRes, err := pdbclient.Get(ctx, path)
if err != nil {
logger.Error("couldn't GET pointer from db", zap.Error(err))
} else {
logger.Info("Success: got Pointer from db",
zap.String("pointer", getRes.String()),
)
}
// Example List with pagination
prefix := p.New("fold1")
items, more, err := pdbclient.List(ctx, prefix, nil, nil, true, 1, meta.None)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("failed to list file paths", zap.Error(err))
} else {
var stringList []string
for _, item := range items {
stringList = append(stringList, item.Path.String())
}
logger.Debug("Success: listed paths: " + strings.Join(stringList, ", ") + "; more: " + fmt.Sprintf("%t", more))
}
// Example Delete
err = pdbclient.Delete(ctx, path)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("Error in deleteing file from db", zap.Error(err))
} else {
logger.Debug("Success: file is deleted from db")
}
}