本文整理汇总了Golang中google/golang.org/api/option.WithHTTPClient函数的典型用法代码示例。如果您正苦于以下问题:Golang WithHTTPClient函数的具体用法?Golang WithHTTPClient怎么用?Golang WithHTTPClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了WithHTTPClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Setup
func Setup(t *testing.T) *storage.Client {
if testProject == "" || testBucket == "" {
t.Skip("TESTPROJECT, and TESTBUCKET EnvVars must be set to perform integration test")
}
gcsctx := &cloudstorage.CloudStoreContext{
LogggingContext: "testing-config",
TokenSource: cloudstorage.GCEDefaultOAuthToken,
Project: testProject,
Bucket: testBucket,
}
// Create http client with Google context auth
googleClient, err := cloudstorage.NewGoogleClient(gcsctx)
if err != nil {
t.Errorf("Failed to create Google Client: %v\n", err)
}
gsc, err := storage.NewClient(context.Background(), option.WithHTTPClient(googleClient.Client()))
if err != nil {
t.Errorf("Error creating Google cloud storage client. project:%s gs://%s/ err:%v\n",
gcsctx.Project, gcsctx.Bucket, err)
}
if gsc == nil {
t.Errorf("storage Client returned is nil!")
}
return gsc
}
示例2: 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, option.WithHTTPClient(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
}
示例3: newTestClient
func newTestClient(rt http.RoundTripper) *Client {
t, err := NewClient(context.Background(), testProjectID, option.WithHTTPClient(&http.Client{Transport: rt}))
if err != nil {
panic(err)
}
return t
}
示例4: NewClient
// NewClient creates a new PubSub client.
func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error) {
var o []option.ClientOption
// Environment variables for gcloud emulator:
// https://option.google.com/sdk/gcloud/reference/beta/emulators/pubsub/
if addr := os.Getenv("PUBSUB_EMULATOR_HOST"); addr != "" {
o = []option.ClientOption{
option.WithEndpoint("http://" + addr + "/"),
option.WithHTTPClient(http.DefaultClient),
}
} else {
o = []option.ClientOption{
option.WithEndpoint(prodAddr),
option.WithScopes(raw.PubsubScope, raw.CloudPlatformScope),
option.WithUserAgent(userAgent),
}
}
o = append(o, opts...)
httpClient, endpoint, err := transport.NewHTTPClient(ctx, o...)
if err != nil {
return nil, fmt.Errorf("dialing: %v", err)
}
s, err := newPubSubService(httpClient, endpoint)
if err != nil {
return nil, fmt.Errorf("constructing pubsub client: %v", err)
}
c := &Client{
projectID: projectID,
s: s,
}
return c, nil
}
示例5: TestEmptyBucketIterator
// Test that BucketIterator's Next method correctly terminates if there is
// nothing to iterate over.
func TestEmptyBucketIterator(t *testing.T) {
hClient, close := newTestServer(func(w http.ResponseWriter, r *http.Request) {
io.Copy(ioutil.Discard, r.Body)
fmt.Fprintf(w, "{}")
})
defer close()
ctx := context.Background()
client, err := NewClient(ctx, option.WithHTTPClient(hClient))
if err != nil {
t.Fatal(err)
}
it := client.Buckets(ctx, "project")
c := make(chan error, 1)
go func() {
_, err := it.Next()
c <- err
}()
select {
case err := <-c:
if err != iterator.Done {
t.Errorf("got %v, want Done", err)
}
case <-time.After(50 * time.Millisecond):
t.Error("timed out")
}
}
示例6: TestEncryption
func TestEncryption(t *testing.T) {
ctx := context.Background()
ft := &fakeTransport{}
hc := &http.Client{Transport: ft}
client, err := NewClient(ctx, option.WithHTTPClient(hc))
if err != nil {
t.Fatalf("error when creating client: %v", err)
}
obj := client.Bucket("bucketname").Object("filename1")
key := []byte("secret-key-that-is-32-bytes-long")
wc := obj.Key(key).NewWriter(ctx)
// TODO(jba): use something other than fakeTransport, which always returns error.
wc.Write([]byte("hello world"))
wc.Close()
if got, want := ft.gotReq.Header.Get("x-goog-encryption-algorithm"), "AES256"; got != want {
t.Errorf("algorithm: got %q, want %q", got, want)
}
gotKey, err := base64.StdEncoding.DecodeString(ft.gotReq.Header.Get("x-goog-encryption-key"))
if err != nil {
t.Fatalf("decoding key: %v", err)
}
if !reflect.DeepEqual(gotKey, key) {
t.Errorf("key: got %v, want %v", gotKey, key)
}
wantHash := sha256.Sum256(key)
gotHash, err := base64.StdEncoding.DecodeString(ft.gotReq.Header.Get("x-goog-encryption-key-sha256"))
if err != nil {
t.Fatalf("decoding hash: %v", err)
}
if !reflect.DeepEqual(gotHash, wantHash[:]) { // wantHash is an array
t.Errorf("hash: got\n%v, want\n%v", gotHash, wantHash)
}
}
示例7: upload
func upload(srcPath string) {
if !*flagUpload {
return
}
destName := strings.Replace(filepath.Base(srcPath), "camlistore", "camlistore-"+releaseDate.Format(fileDateFormat), 1)
versionedTarball := "monthly/" + destName
log.Printf("Uploading %s/%s ...", bucket, versionedTarball)
ts, err := tokenSource(bucket)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
stoClient, err := storage.NewClient(ctx, option.WithTokenSource(ts), option.WithHTTPClient(oauth2.NewClient(ctx, ts)))
if err != nil {
log.Fatal(err)
}
w := stoClient.Bucket(bucket).Object(versionedTarball).NewWriter(ctx)
w.ACL = publicACL(project)
w.CacheControl = "no-cache" // TODO: remove for non-tip releases? set expirations?
contentType := "application/x-gtar"
if strings.HasSuffix(versionedTarball, ".zip") {
contentType = "application/zip"
}
w.ContentType = contentType
csw := sha256.New()
mw := io.MultiWriter(w, csw)
src, err := os.Open(srcPath)
if err != nil {
log.Fatal(err)
}
defer src.Close()
if _, err := io.Copy(mw, src); err != nil {
log.Fatalf("io.Copy: %v", err)
}
if err := w.Close(); err != nil {
log.Fatalf("closing GCS storage writer: %v", err)
}
log.Printf("Uploaded monthly tarball to %s", versionedTarball)
// And upload the corresponding checksum
checkSumFile := versionedTarball + ".sha256"
sum := fmt.Sprintf("%x", csw.Sum(nil))
w = stoClient.Bucket(bucket).Object(checkSumFile).NewWriter(ctx)
w.ACL = publicACL(project)
w.CacheControl = "no-cache" // TODO: remove for non-tip releases? set expirations?
w.ContentType = "text/plain"
if _, err := io.Copy(w, strings.NewReader(sum)); err != nil {
log.Fatalf("error uploading checksum %v: %v", checkSumFile, err)
}
if err := w.Close(); err != nil {
log.Fatalf("closing GCS storage writer: %v", err)
}
log.Printf("Uploaded monthly tarball checksum to %s", checkSumFile)
}
示例8: uploadReleaseTarball
// uploadReleaseTarball uploads the generated tarball of binaries in
// camlistore-release/VERSION/camlistoreVERSION-REV-CONTENTS.EXT. It then makes a copy in
// the same bucket and path, as camlistoreVERSION-CONTENTS.EXT.
func uploadReleaseTarball() {
proj := "camlistore-website"
bucket := "camlistore-release"
tarball := *flagVersion + "/" + filepath.Base(releaseTarball)
versionedTarball := strings.Replace(tarball, "camlistore"+*flagVersion, "camlistore"+*flagVersion+"-"+rev(), 1)
log.Printf("Uploading %s/%s ...", bucket, versionedTarball)
ts, err := tokenSource(bucket)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
stoClient, err := storage.NewClient(ctx, option.WithTokenSource(ts), option.WithHTTPClient(oauth2.NewClient(ctx, ts)))
if err != nil {
log.Fatal(err)
}
w := stoClient.Bucket(bucket).Object(versionedTarball).NewWriter(ctx)
w.ACL = publicACL(proj)
w.CacheControl = "no-cache" // TODO: remove for non-tip releases? set expirations?
contentType := "application/x-gtar"
if *buildOS == "windows" {
contentType = "application/zip"
}
w.ContentType = contentType
src, err := os.Open(releaseTarball)
if err != nil {
log.Fatal(err)
}
defer src.Close()
if _, err := io.Copy(w, src); err != nil {
log.Fatalf("io.Copy: %v", err)
}
if err := w.Close(); err != nil {
log.Fatalf("closing GCS storage writer: %v", err)
}
log.Printf("Uploaded tarball to %s", versionedTarball)
if !isWIP() {
log.Printf("Copying tarball to %s/%s ...", bucket, tarball)
dest := stoClient.Bucket(bucket).Object(tarball)
if _, err := stoClient.Bucket(bucket).Object(versionedTarball).CopyTo(
ctx,
dest,
&storage.ObjectAttrs{
ACL: publicACL(proj),
ContentType: contentType,
}); err != nil {
log.Fatalf("Error uploading %v: %v", tarball, err)
}
log.Printf("Uploaded tarball to %s", tarball)
}
}
示例9: TestTranslateURL
func TestTranslateURL(t *testing.T) {
// The translate API has all inputs in the URL.
// Make sure we generate the right one.
ctx := context.Background()
ft := &fakeTransport{}
c, err := NewClient(ctx, option.WithHTTPClient(&http.Client{Transport: ft}))
if err != nil {
t.Fatal(err)
}
for _, test := range []struct {
target language.Tag
inputs []string
opts *Options
want url.Values
}{
{language.Spanish, []string{"text"}, nil, url.Values{
"q": []string{"text"},
"target": []string{"es"},
}},
{language.English, []string{"text"}, &Options{}, url.Values{
"q": []string{"text"},
"target": []string{"en"},
}},
{language.Turkish, []string{"t1", "t2"}, nil, url.Values{
"q": []string{"t1", "t2"},
"target": []string{"tr"},
}},
{language.English, []string{"text"}, &Options{Source: language.French},
url.Values{
"q": []string{"text"},
"source": []string{"fr"},
"target": []string{"en"},
},
},
{language.English, []string{"text"}, &Options{Source: language.French, Format: HTML}, url.Values{
"q": []string{"text"},
"source": []string{"fr"},
"format": []string{"html"},
"target": []string{"en"},
}},
} {
_, err = c.Translate(ctx, test.inputs, test.target, test.opts)
if err != nil {
t.Fatal(err)
}
got := ft.req.URL.Query()
test.want.Add("alt", "json")
if !reflect.DeepEqual(got, test.want) {
t.Errorf("Translate(%s, %v, %+v):\ngot %s\nwant %s",
test.target, test.inputs, test.opts, got, test.want)
}
}
}
示例10: uploadBinary
// uploadBinary uploads the currently-running Linux binary.
// It crashes if it fails.
func (cl *cloudLaunch) uploadBinary() {
ctx := context.Background()
if cl.BinaryBucket == "" {
log.Fatal("cloudlaunch: Config.BinaryBucket is empty")
}
stoClient, err := storage.NewClient(ctx, option.WithHTTPClient(cl.oauthClient))
if err != nil {
log.Fatal(err)
}
w := stoClient.Bucket(cl.BinaryBucket).Object(cl.binaryObject()).NewWriter(ctx)
if err != nil {
log.Fatal(err)
}
w.ACL = []storage.ACLRule{
// If you don't give the owners access, the web UI seems to
// have a bug and doesn't have access to see that it's public, so
// won't render the "Shared Publicly" link. So we do that, even
// though it's dumb and unnecessary otherwise:
{
Entity: storage.ACLEntity("project-owners-" + cl.GCEProjectID),
Role: storage.RoleOwner,
},
// Public, so our systemd unit can get it easily:
{
Entity: storage.AllUsers,
Role: storage.RoleReader,
},
}
w.CacheControl = "no-cache"
selfPath := getSelfPath()
log.Printf("Uploading %q to %v", selfPath, cl.binaryURL())
f, err := os.Open(selfPath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
n, err := io.Copy(w, f)
if err != nil {
log.Fatal(err)
}
if err := w.Close(); err != nil {
log.Fatal(err)
}
log.Printf("Uploaded %d bytes", n)
}
示例11: client
// client returns the GCS Storage client instance.
// If there isn't one yet, it tries to create one.
func (bs *GCSBackupStorage) client() (*storage.Client, error) {
bs.mu.Lock()
defer bs.mu.Unlock()
if bs._client == nil {
// FIXME(alainjobart) add context.Context to BackupStorage API.
ctx := context.TODO()
authClient, err := google.DefaultClient(ctx)
if err != nil {
return nil, err
}
client, err := storage.NewClient(ctx, option.WithHTTPClient(authClient))
if err != nil {
return nil, err
}
bs._client = client
}
return bs._client, nil
}
示例12: gcsCommonClient
func gcsCommonClient(client *http.Client, csctx *CloudStoreContext) (Store, error) {
project := csctx.Project
bucket := csctx.Bucket
prefix := fmt.Sprintf("%s:(project=%s bucket=%s)", csctx.LogggingContext, project, bucket)
l := LogConstructor(prefix)
gcs, err := storage.NewClient(context.Background(), option.WithHTTPClient(client))
if err != nil {
l.Errorf("%v error creating Google cloud storage client. project:%s gs://%s/ err:%v ",
csctx.LogggingContext, project, bucket, err)
return nil, err
}
store, err := NewGCSStore(gcs, bucket, csctx.TmpDir, maxResults, l)
if err != nil {
l.Errorf("error creating the store. err=%v ", err)
return nil, err
}
return store, nil
}
示例13: TestErrorOnObjectsInsertCall
func TestErrorOnObjectsInsertCall(t *testing.T) {
ctx := context.Background()
hc := &http.Client{Transport: &fakeTransport{}}
client, err := NewClient(ctx, option.WithHTTPClient(hc))
if err != nil {
t.Fatalf("error when creating client: %v", err)
}
wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
wc.ContentType = "text/plain"
// We can't check that the Write fails, since it depends on the write to the
// underling fakeTransport failing which is racy.
wc.Write([]byte("hello world"))
// Close must always return an error though since it waits for the transport to
// have closed.
if err := wc.Close(); err == nil {
t.Errorf("expected error on close, got nil")
}
}
示例14: init
func init() {
if !metadata.OnGCE() {
return
}
hc, err := google.DefaultClient(oauth2.NoContext)
if err != nil {
registerBrokenFS(fmt.Errorf("could not get http client for context: %v", err))
return
}
ctx := context.Background()
sc, err := storage.NewClient(ctx, option.WithHTTPClient(hc))
if err != nil {
registerBrokenFS(fmt.Errorf("could not get cloud storage client: %v", err))
return
}
wkfs.RegisterFS("/gcs/", &gcsFS{
ctx: ctx,
sc: sc,
})
}
示例15: TestCopyToMissingFields
func TestCopyToMissingFields(t *testing.T) {
var tests = []struct {
srcBucket, srcName, destBucket, destName string
errMsg string
}{
{
"mybucket", "", "mybucket", "destname",
"name is empty",
},
{
"mybucket", "srcname", "mybucket", "",
"name is empty",
},
{
"", "srcfile", "mybucket", "destname",
"name is empty",
},
{
"mybucket", "srcfile", "", "destname",
"name is empty",
},
}
ctx := context.Background()
client, err := NewClient(ctx, option.WithHTTPClient(&http.Client{Transport: &fakeTransport{}}))
if err != nil {
panic(err)
}
for i, test := range tests {
src := client.Bucket(test.srcBucket).Object(test.srcName)
dst := client.Bucket(test.destBucket).Object(test.destName)
_, err := dst.CopierFrom(src).Run(ctx)
if !strings.Contains(err.Error(), test.errMsg) {
t.Errorf("CopyTo test #%v:\ngot err %q\nwant err %q", i, err, test.errMsg)
}
}
}