示例#1
0
func getClient(t *testing.T) *b2.Client {
	accountID := os.Getenv("ACCOUNT_ID")
	applicationKey := os.Getenv("APPLICATION_KEY")
	if accountID == "" || applicationKey == "" {
		t.Fatal("Missing ACCOUNT_ID or APPLICATION_KEY")
	}
	clientMu.Lock()
	defer clientMu.Unlock()
	if client != nil {
		return client
	}
	c, err := b2.NewClient(accountID, applicationKey, &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyFromEnvironment,
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		},
	})
	if err != nil {
		t.Fatal("While authenticating:", err)
	}
	client = c
	return c
}
示例#2
0
func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
	var (
		auth      = config.RequiredObject("auth")
		bucket    = config.RequiredString("bucket")
		cacheSize = config.OptionalInt64("cacheSize", 32<<20)

		accountID = auth.RequiredString("account_id")
		appKey    = auth.RequiredString("application_key")
	)

	if err := config.Validate(); err != nil {
		return nil, err
	}
	if err := auth.Validate(); err != nil {
		return nil, err
	}

	var dirPrefix string
	if parts := strings.SplitN(bucket, "/", 2); len(parts) > 1 {
		dirPrefix = parts[1]
		bucket = parts[0]
	}
	if dirPrefix != "" && !strings.HasSuffix(dirPrefix, "/") {
		dirPrefix += "/"
	}

	cl, err := b2.NewClient(accountID, appKey, nil)
	if err != nil {
		return nil, err
	}
	b, err := cl.BucketByName(bucket, true)
	if err != nil {
		return nil, err
	}

	s := &Storage{
		cl: cl, b: b,
		dirPrefix: dirPrefix,
	}

	if cacheSize != 0 {
		s.cache = memory.NewCache(cacheSize)
	}

	return s, nil
}