本文整理汇总了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")
}
}
示例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()
}
示例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()
}
示例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
}
示例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("", "", " ")
}
示例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()
}
示例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
}
示例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()
}
示例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()
}
示例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)
}
}
示例11: GetNewStorageClient
func GetNewStorageClient() (*StorageClient, error) {
client, err := storage.NewClient(ctx, cloud.WithTokenSource(defaultTokenSource))
return &StorageClient{
Client: client,
Ctx: ctx,
}, err
}
示例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()
}
示例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)
}
示例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
}
示例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
}