Skip to content

Commit

Permalink
Merge pull request #220 from stephenplaza/master
Browse files Browse the repository at this point in the history
Adds readonly gbucket support that does not require credentials
  • Loading branch information
DocSavage authored Apr 25, 2017
2 parents 9a1b6d9 + 8c0e3c8 commit 4aba8ac
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions storage/gbucket/gbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"runtime"
"sort"
"sync"
Expand All @@ -36,6 +37,8 @@ import (

api "cloud.google.com/go/storage"
"golang.org/x/net/context"
"google.golang.org/api/option"
"net/http"
)

func init() {
Expand All @@ -53,6 +56,12 @@ const (
INITKEY = "initialized"
// total connection tries
NUM_TRIES = 3

// current version of gbucket (must be >1 byte string)
CURVER = "1.0"

// first gbucket version (must be >1 byte string)
ORIGVER = "1.0"
)

// --- Engine Implementation ------
Expand Down Expand Up @@ -113,7 +122,13 @@ func (e *Engine) newGBucket(config dvid.StoreConfig) (*GBucket, bool, error) {
}

// NewClient uses Application Default Credentials to authenticate.
gb.client, err = api.NewClient(gb.ctx)
credval := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if credval == "" {
gb.client, err = api.NewClient(gb.ctx, option.WithHTTPClient(http.DefaultClient))
} else {
gb.client, err = api.NewClient(gb.ctx)
}

if err != nil {
return nil, false, err
}
Expand All @@ -131,10 +146,17 @@ func (e *Engine) newGBucket(config dvid.StoreConfig) (*GBucket, bool, error) {
// check if value exists
if val == nil {
created = true
err = gb.putV(storage.Key(INITKEY), make([]byte, 1))

err = gb.putV(storage.Key(INITKEY), []byte(CURVER))
if err != nil {
return nil, false, err
}
gb.version = CURVER
} else if len(val) == 1 {
// set default version (versionless original wrote out 1 byte)
gb.version = ORIGVER
} else {
gb.version = string(val)
}

// if we know it's newly created, just return.
Expand Down Expand Up @@ -164,6 +186,7 @@ type GBucket struct {
activeRequests chan interface{}
ctx context.Context
client *api.Client
version string
}

func (db *GBucket) String() string {
Expand Down

0 comments on commit 4aba8ac

Please sign in to comment.