当前位置: 首页>>代码示例>>Golang>>正文


Golang storage.NewClient函数代码示例

本文整理汇总了Golang中google/golang.org/cloud/storage.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: listFiles

func (d *demo) listFiles() {
	io.WriteString(d.res, "\nRETRIEVING FILE NAMES\n")

	client, err := storage.NewClient(d.ctx)
	if err != nil {
		log.Errorf(d.ctx, "%v", err)
		return
	}
	defer client.Close()

	// create a query
	q := storage.Query{
		MaxResults: 10,
	}

	objs, err := client.Bucket(gcsBucket).List(d.ctx, &q)
	if err != nil {
		log.Errorf(d.ctx, "%v", err)
		return
	}

	for _, obj := range objs.Results {
		io.WriteString(d.res, obj.Name+"\n")
	}
}
开发者ID:robertg231,项目名称:CSCI-130-HW,代码行数:25,代码来源:main.go

示例2: main

func main(response http.ResponseWriter, request *http.Request) {
	if request.URL.Path != "/" {
		http.NotFound(response, request)
		return
	}

	ctx := appengine.NewContext(request)
	client, err := storage.NewClient(ctx)
	if err != nil {
		log.Errorf(ctx, "*** ERROR: main NewClient: ", err, " ***")
	}
	defer client.Close()

	a := &app{
		ctx:      ctx,
		response: response,
		request:  request,
		bucket:   client.Bucket(bucket),
		client:   client,
	}

	a.createFiles()
	a.showFiles()
	a.query_delimiter()
}
开发者ID:jgtaruc,项目名称:GoLang130,代码行数:25,代码来源:main.go

示例3: handler

func handler(w http.ResponseWriter, r *http.Request) {
	context := appengine.NewContext(r)

	if r.URL.Path != "/" {
		http.NotFound(w, r)
		return
	}

	client, err := storage.NewClient(context)
	if err != nil {
		log.Errorf(context, "error handler newClient: ", err)
		return
	}

	defer client.Close()

	d := &demo{
		context: context,
		w:       w,
		client:  client,
		bucket:  client.Bucket(gcsBucket),
	}

	d.createFiles()
	d.listFiles()
	io.WriteString(d.w, "\nResult From ListDir - With Delimiter\n")
	d.listDir()

}
开发者ID:deepthi261,项目名称:130,代码行数:29,代码来源:main.go

示例4: getInstalledTLS

// getInstalledTLS returns the TLS certificate and key stored on Google Cloud Storage for the
// instance defined in d.Conf.
//
// If either the TLS keypair doesn't exist, the error is os.ErrNotExist.
func (d *Deployer) getInstalledTLS() (certPEM, keyPEM []byte, err error) {
	ctx := context.Background()
	stoClient, err := cloudstorage.NewClient(ctx, cloud.WithBaseHTTP(d.Client))
	if err != nil {
		return nil, nil, fmt.Errorf("error creating Cloud Storage client to fetch TLS cert & key from new instance: %v", err)
	}
	getFile := func(name string) ([]byte, error) {
		sr, err := stoClient.Bucket(d.Conf.bucketBase()).Object(path.Join(configDir, name)).NewReader(ctx)
		if err == cloudstorage.ErrObjectNotExist {
			return nil, os.ErrNotExist
		}
		if err != nil {
			return nil, err
		}
		defer sr.Close()
		return ioutil.ReadAll(sr)
	}
	var grp syncutil.Group
	grp.Go(func() (err error) {
		certPEM, err = getFile(certFilename())
		return
	})
	grp.Go(func() (err error) {
		keyPEM, err = getFile(keyFilename())
		return
	})
	err = grp.Err()
	return
}
开发者ID:pombredanne,项目名称:camlistore,代码行数:33,代码来源:deploy.go

示例5: handler

func handler(res http.ResponseWriter, req *http.Request) {

	if req.URL.Path != "/" {
		http.NotFound(res, req)
		return
	}
	ctx := appengine.NewContext(req)
	client, err := storage.NewClient(ctx)
	if err != nil {
		log.Errorf(ctx, "ERROR handler NewClient: ", err)
		return
	}
	defer client.Close()

	d := &gcsDelimit{
		ctx:    ctx,
		res:    res,
		client: client,
		bucket: client.Bucket(gcsBucket),
	}

	d.createFiles()
	d.listFiles()
	io.WriteString(d.res, "\nResults from list directory **with** delimiter\n")
	d.listDir("", "/", "  ")
	io.WriteString(d.res, "\nResults from list directory **without** delimiter\n")
	d.listDir("", "", "  ")

}
开发者ID:yash0690,项目名称:Golang,代码行数:29,代码来源:main.go

示例6: listBucket

func (s *Session) listBucket() {

	client, err := storage.NewClient(s.ctx)
	if err != nil {
		log.Errorf(s.ctx, "ERROR listBucket storage.NewClient: %v", err)
		return
	}
	defer client.Close()

	q := &storage.Query{
		Prefix: s.ID,
	}

	objs, err := client.Bucket(gcsBucket).List(s.ctx, q)
	if err != nil {
		log.Errorf(s.ctx, "ERROR listBucket client.Bucket: %v", err)
		return
	}

	for _, obj := range objs.Results {
		s.Pictures[obj.Name] = obj.Name
	}

	s.putSession()
}
开发者ID:GoesToEleven,项目名称:golang-web,代码行数:25,代码来源:storage.go

示例7: listBucket

func listBucket(ctx context.Context, bucketName, folder string) ([]string, []string, error) {
	client, err := storage.NewClient(ctx)
	if err != nil {
		return nil, nil, err
	}
	defer client.Close()

	var files, folders []string

	query := &storage.Query{
		Delimiter: "/",
		Prefix:    folder,
	}
	// objs is *storage.Objects
	objs, err := client.Bucket(bucketName).List(ctx, query)
	if err != nil {
		return nil, nil, err
	}

	for _, subfolder := range objs.Prefixes {
		folders = append(folders, subfolder[len(folder):])
	}

	for _, obj := range objs.Results {
		files = append(files, obj.Name)
	}

	return files, folders, nil
}
开发者ID:GoesToEleven,项目名称:GolangTraining,代码行数:29,代码来源:storage.go

示例8: handler

func handler(res http.ResponseWriter, req *http.Request) {

	// Saving the file
	ctx := appengine.NewContext(req)
	client, err := storage.NewClient(ctx)
	logStorageError(ctx, "Could not create a new client", err)
	defer client.Close()

	writer := client.Bucket(BUCKET_NAME).Object(FILE_NAME).NewWriter(ctx)
	writer.ACL = []storage.ACLRule{{
		storage.AllUsers,
		storage.RoleReader}}

	// Reading the file from disk
	reader, err := os.Open(FILE_NAME)
	logError(err)
	io.Copy(writer, reader)
	writer.Close()

	// Reading the file
	rd, err := client.Bucket(BUCKET_NAME).Object(FILE_NAME).NewReader(ctx)
	logError(err)
	io.Copy(res, rd)
	defer rd.Close()
}
开发者ID:momazia,项目名称:GoTraining,代码行数:25,代码来源:main.go

示例9: Example_auth

func Example_auth() {
	// Initialize an authorized context with Google Developers Console
	// JSON key. Read the google package examples to learn more about
	// different authorization flows you can use.
	// http://godoc.org/golang.org/x/oauth2/google
	jsonKey, err := ioutil.ReadFile("/path/to/json/keyfile.json")
	if err != nil {
		log.Fatal(err)
	}
	conf, err := google.JWTConfigFromJSON(
		jsonKey,
		storage.ScopeFullControl,
	)
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	client, err := storage.NewClient(ctx, cloud.WithTokenSource(conf.TokenSource(ctx)))
	if err != nil {
		log.Fatal(err)
	}

	// Use the client (see other examples)
	doSomething(client)

	// After using the client, free any resources (e.g. network connections).
	client.Close()
}
开发者ID:nhr,项目名称:origin,代码行数:28,代码来源:example_test.go

示例10: statFiles

func (d *demo) statFiles() {
	io.WriteString(d.res, "\nRETRIEVING FILE STATS\n")

	client, err := storage.NewClient(d.ctx)
	if err != nil {
		log.Errorf(d.ctx, "%v", err)
		return
	}
	defer client.Close()

	// create a query
	q := storage.Query{
		MaxResults: 2,
	}

	// instead of nil
	// now passing in a *storage.Query
	objs, err := client.Bucket(gcsBucket).List(d.ctx, &q)
	if err != nil {
		log.Errorf(d.ctx, "%v", err)
		return
	}

	for _, v := range objs.Results {
		d.statFile(v.Name)
	}
}
开发者ID:Adrian651,项目名称:CSCI-130-,代码行数:27,代码来源:main.go

示例11: GetNewStorageClient

func GetNewStorageClient() (*StorageClient, error) {
	client, err := storage.NewClient(ctx, cloud.WithTokenSource(defaultTokenSource))
	return &StorageClient{
		Client: client,
		Ctx:    ctx,
	}, err
}
开发者ID:mshockwave,项目名称:nthuaplus-backend,代码行数:7,代码来源:storage.go

示例12: handler

func handler(res http.ResponseWriter, req *http.Request) {

	if req.URL.Path != "/" {
		http.NotFound(res, req)
		return
	}

	ctx := appengine.NewContext(req)

	client, err := storage.NewClient(ctx)
	if err != nil {
		log.Errorf(ctx, "ERROR handler NewClient: ", err)
		return
	}
	defer client.Close()

	d := &demo{
		ctx:    ctx,
		res:    res,
		client: client,
		bucket: client.Bucket(gcsBucket),
	}

	res.Header().Set("Content-Language", "en")

	d.createFiles()
	d.listFiles()
}
开发者ID:GoesToEleven,项目名称:golang-web,代码行数:28,代码来源:main.go

示例13: main

func main() {
	log.SetFlags(0)
	plugin.Param("workspace", &workspace)
	plugin.Param("build", &build)
	plugin.Param("repo", &repo)
	plugin.Param("vargs", &vargs)
	plugin.MustParse()
	sort.Strings(vargs.Gzip) // need for matchGzip

	// context for all clients
	ctx := context.Background()
	// GitHub client
	gts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: vargs.GitHubToken})
	client.ghub = github.NewClient(oauth2.NewClient(ctx, gts))
	// GCS client
	auth, err := google.JWTConfigFromJSON([]byte(vargs.AuthKey), storage.ScopeFullControl)
	if err != nil {
		fatalf("auth: %v", err)
	}
	tsrc := auth.TokenSource(ctx)
	client.gcs, err = storage.NewClient(ctx, cloud.WithTokenSource(auth.TokenSource(ctx)))
	if err != nil {
		fatalf("storage client: %v", err)
	}
	// http client with service account authorization
	client.http = oauth2.NewClient(ctx, tsrc)

	run()
	if ecode != 0 {
		msg := fmt.Sprintf("exited with code %d", ecode)
		updateStatus("error", msg, stagingURL)
	}
	os.Exit(ecode)
}
开发者ID:peterwang1996,项目名称:WebFundamentals,代码行数:34,代码来源:main.go

示例14: Store

// Store binary data to GCS
func Store(c context.Context, data []byte, fileName, mimeType, bucketName string) (absFilename string, err error) {
	if bucketName == "" {
		var err error
		if bucketName, err = file.DefaultBucketName(c); err != nil {
			log.Errorf(c, "failed to get default GCS bucket name: %v", err)
			return "", err
		}
	}

	client, err := storage.NewClient(c)
	if err != nil {
		log.Errorf(c, "failed to create storage client: %v", err)
		return "", err
	}
	defer client.Close()

	wc := client.Bucket(bucketName).Object(fileName).NewWriter(c)
	wc.ContentType = mimeType

	if _, err := wc.Write(data); err != nil {
		log.Errorf(c, "upload file: unable to write data to bucket %q, file %q: %v", bucketName, fileName, err)
		return "", err
	}
	if err := wc.Close(); err != nil {
		log.Errorf(c, "upload file: unable to close bucket %q, file %q: %v", bucketName, fileName, err)
		return "", err
	}

	return getAbsFilename(bucketName, fileName), nil
}
开发者ID:knightso,项目名称:caterpillar,代码行数:31,代码来源:gcs.go

示例15: tlsCertFromGCS

func tlsCertFromGCS(ctx context.Context) (*tls.Certificate, error) {
	sc, err := storage.NewClient(ctx)
	if err != nil {
		return nil, err
	}
	slurp := func(key string) ([]byte, error) {
		const bucket = "camlistore-website-resource"
		rc, err := sc.Bucket(bucket).Object(key).NewReader(ctx)
		if err != nil {
			return nil, fmt.Errorf("Error fetching GCS object %q in bucket %q: %v", key, bucket, err)
		}
		defer rc.Close()
		return ioutil.ReadAll(rc)
	}
	certPem, err := slurp("ssl.crt")
	if err != nil {
		return nil, err
	}
	keyPem, err := slurp("ssl.key")
	if err != nil {
		return nil, err
	}
	cert, err := tls.X509KeyPair(certPem, keyPem)
	if err != nil {
		return nil, err
	}
	return &cert, nil
}
开发者ID:stevearm,项目名称:camlistore,代码行数:28,代码来源:camweb.go


注:本文中的google/golang.org/cloud/storage.NewClient函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。