本文整理汇总了Golang中google/golang.org/cloud.WithBaseHTTP函数的典型用法代码示例。如果您正苦于以下问题:Golang WithBaseHTTP函数的具体用法?Golang WithBaseHTTP怎么用?Golang WithBaseHTTP使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了WithBaseHTTP函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: 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
示例2: newTestClient
func newTestClient(rt http.RoundTripper) *trace.Client {
t, err := trace.NewClient(context.Background(), testProjectID, cloud.WithBaseHTTP(&http.Client{Transport: rt}))
if err != nil {
panic(err)
}
return t
}
开发者ID:trythings,项目名称:trythings,代码行数:7,代码来源:trace_test.go
示例3: TestCopyObjectMissingFields
func TestCopyObjectMissingFields(t *testing.T) {
var tests = []struct {
srcBucket, srcName, destBucket, destName string
errMsg string
}{
{
"mybucket", "", "mybucket", "destname",
"srcName and destName must be non-empty",
},
{
"mybucket", "srcname", "mybucket", "",
"srcName and destName must be non-empty",
},
{
"", "srcfile", "mybucket", "destname",
"srcBucket and destBucket must both be non-empty",
},
{
"mybucket", "srcfile", "", "destname",
"srcBucket and destBucket must both be non-empty",
},
}
ctx := context.Background()
client, err := NewClient(ctx, cloud.WithBaseHTTP(&http.Client{Transport: &fakeTransport{}}))
if err != nil {
panic(err)
}
for i, test := range tests {
_, err := client.CopyObject(ctx, test.srcBucket, test.srcName, test.destBucket, test.destName, nil)
if !strings.Contains(err.Error(), test.errMsg) {
t.Errorf("CopyObject test #%v: err = %v, want %v", i, err, test.errMsg)
}
}
}
开发者ID:Celluliodio,项目名称:flannel,代码行数:34,代码来源:storage_test.go
示例4: NewClient
// NewClient creates a new PubSub client.
func NewClient(ctx context.Context, projectID string, opts ...cloud.ClientOption) (*Client, error) {
var o []cloud.ClientOption
// Environment variables for gcloud emulator:
// https://cloud.google.com/sdk/gcloud/reference/beta/emulators/pubsub/
if addr := os.Getenv("PUBSUB_EMULATOR_HOST"); addr != "" {
o = []cloud.ClientOption{
cloud.WithEndpoint("http://" + addr + "/"),
cloud.WithBaseHTTP(http.DefaultClient),
}
} else {
o = []cloud.ClientOption{
cloud.WithEndpoint(prodAddr),
cloud.WithScopes(raw.PubsubScope, raw.CloudPlatformScope),
cloud.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
}
开发者ID:Ropes,项目名称:pubbing,代码行数:35,代码来源:pubsub.go
示例5: upload
func upload(srcPath string) {
if !*flagUpload {
return
}
destName := strings.Replace(filepath.Base(srcPath), "camlistore", "camlistore-"+rev(), 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, cloud.WithTokenSource(ts), cloud.WithBaseHTTP(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)
}
开发者ID:pombredanne,项目名称:camlistore,代码行数:58,代码来源:monthly.go
示例6: 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, cloud.WithTokenSource(ts), cloud.WithBaseHTTP(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)
}
}
开发者ID:pombredanne,项目名称:camlistore,代码行数:57,代码来源:dock.go
示例7: setupOAuth
func setupOAuth() error {
client, err := auth.NewDefaultJWTServiceAccountClient(auth.SCOPE_READ_WRITE)
if err != nil {
return fmt.Errorf("Problem setting up client OAuth: %v", err)
}
if storageClient, err = storage.NewClient(context.Background(), cloud.WithBaseHTTP(client)); err != nil {
return fmt.Errorf("Problem authenticating: %v", err)
}
return nil
}
开发者ID:saltmueller,项目名称:skia-buildbot,代码行数:11,代码来源:main.go
示例8: TestErrorOnObjectsInsertCall
func TestErrorOnObjectsInsertCall(t *testing.T) {
ctx := context.Background()
hc := &http.Client{Transport: &fakeTransport{}}
client, err := NewClient(ctx, cloud.WithBaseHTTP(hc))
if err != nil {
t.Errorf("error when creating client: %v", err)
}
wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
wc.ContentType = "text/plain"
if _, err := wc.Write([]byte("hello world")); err == nil {
t.Errorf("expected error on write, got nil")
}
if err := wc.Close(); err == nil {
t.Errorf("expected error on close, got nil")
}
}
开发者ID:rfistman,项目名称:camlistore,代码行数:16,代码来源:types_test.go
示例9: deleteFolderContent
// deleteFolderContent removes all content ing the given GS bucket/foldername.
func deleteFolderContent(t *testing.T, bucket, folderName string, client *http.Client) {
ctx := context.Background()
cStorage, err := storage.NewClient(ctx, cloud.WithBaseHTTP(client))
assert.Nil(t, err)
bucketHdl := cStorage.Bucket(bucket)
objList, err := bucketHdl.List(ctx, &storage.Query{Prefix: folderName, MaxResults: 1000})
assert.Nil(t, err)
for _, obj := range objList.Results {
fmt.Printf("%s/%s\n\n", obj.Bucket, obj.Name)
objHdl := bucketHdl.Object(obj.Name)
assert.Nil(t, objHdl.Delete(ctx))
fmt.Printf("Deleted object %s / %s\n\n", obj.Bucket, obj.Name)
}
}
开发者ID:saltmueller,项目名称:skia-buildbot,代码行数:17,代码来源:pdfingestion_test.go
示例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, cloud.WithBaseHTTP(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)
}
开发者ID:edrex-duex,项目名称:go4,代码行数:47,代码来源:cloudlaunch.go
示例11: 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(), cloud.WithBaseHTTP(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
}
开发者ID:Ropes,项目名称:pubbing,代码行数:19,代码来源:store.go
示例12: CreateGooglePubSubClient
// CreateGooglePubSubClient returns client communicate with google Pub/Sub service
func CreateGooglePubSubClient() (*pubsub.Client, error) {
envValue := os.Getenv(NDGooglePubSubURL)
if envValue != "" {
googlePubSubURL = envValue
}
envValue = os.Getenv(NDGoogleProjectID)
if envValue != "" {
googleProjectID = envValue
}
ctx := cloud.NewContext(googleProjectID, http.DefaultClient)
o := []cloud.ClientOption{
cloud.WithBaseHTTP(http.DefaultClient),
cloud.WithEndpoint(googleDatastoreURL),
}
client, _ := pubsub.NewClient(ctx, googleProjectID, o...)
return client, nil
}
开发者ID:nildev,项目名称:prj-tpl-basic-api,代码行数:20,代码来源:registry.go
示例13: TestErrorOnObjectsInsertCall
func TestErrorOnObjectsInsertCall(t *testing.T) {
ctx := context.Background()
hc := &http.Client{Transport: &fakeTransport{}}
client, err := NewClient(ctx, cloud.WithBaseHTTP(hc))
if err != nil {
t.Errorf("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")
}
}
开发者ID:Celluliodio,项目名称:flannel,代码行数:20,代码来源:writer_test.go
示例14: newPDFProcessor
// newPDFProcessor implements the ingestion.Constructor signature.
func newPDFProcessor(vcs vcsinfo.VCS, config *sharedconfig.IngesterConfig, client *http.Client) (ingestion.Processor, error) {
// Parse the parameters right into the pdfProcessor instance.
ret := &pdfProcessor{}
err := combineErrors(
ensureNonEmptyKey(config.ExtraParams, CONFIG_INPUT_IMAGES_BUCKET, &ret.inImagesBucket),
ensureNonEmptyKey(config.ExtraParams, CONFIG_INPUT_IMAGES_DIR, &ret.inImagesDir),
ensureNonEmptyKey(config.ExtraParams, CONFIG_OUTPUT_JSON_BUCKET, &ret.outJsonBucket),
ensureNonEmptyKey(config.ExtraParams, CONFIG_OUTPUT_JSON_DIR, &ret.outJsonDir),
ensureNonEmptyKey(config.ExtraParams, CONFIG_OUTPUT_IMAGES_BUCKET, &ret.outImagesBucket),
ensureNonEmptyKey(config.ExtraParams, CONFIG_OUTPUT_IMAGES_DIR, &ret.outImagesDir))
if err != nil {
return nil, err
}
// Make sure there is a cachedir.
ret.pdfCacheDir = config.ExtraParams[CONFIG_PDF_CACHEDIR]
if ret.pdfCacheDir == "" {
if ret.pdfCacheDir, err = ioutil.TempDir("", "pdfcache-dir"); err != nil {
return nil, err
}
}
// Create the storage service client.
storageClient, err := storage.NewClient(context.Background(), cloud.WithBaseHTTP(client))
if err != nil {
return nil, fmt.Errorf("Failed to create a Google Storage API client: %s", err)
}
// Find rasterizers that have been enabled by the PDF packaged.
rasterizers := pdf.GetEnabledRasterizers()
if len(rasterizers) == 0 {
return nil, fmt.Errorf("No rasterizers enabled.")
}
ret.client = client
ret.storageClient = storageClient
ret.rasterizers = rasterizers
return ret, nil
}
开发者ID:saltmueller,项目名称:skia-buildbot,代码行数:40,代码来源:pdfingestion.go
示例15: CreateGoogleDatastoreClient
// CreateGoogleDatastoreClient create client to communicate to google Datastore service
func CreateGoogleDatastoreClient() (*datastore.Client, error) {
envValue := os.Getenv(NDGoogleDatastoreURL)
if envValue != "" {
googleDatastoreURL = envValue
}
envValue = os.Getenv(NDGoogleProjectID)
if envValue != "" {
googleProjectID = envValue
}
ctx := cloud.NewContext(googleProjectID, http.DefaultClient)
o := []cloud.ClientOption{
cloud.WithBaseHTTP(http.DefaultClient),
cloud.WithEndpoint(googleDatastoreURL),
}
client, err := datastore.NewClient(ctx, googleProjectID, o...)
if err != nil {
return nil, err
}
return client, nil
}
开发者ID:nildev,项目名称:prj-tpl-basic-api,代码行数:24,代码来源:registry.go
示例16: TestCopyToMissingFields
func TestCopyToMissingFields(t *testing.T) {
var tests = []struct {
srcBucket, srcName, destBucket, destName string
errMsg string
}{
{
"mybucket", "", "mybucket", "destname",
"the source and destination object names must both be non-empty",
},
{
"mybucket", "srcname", "mybucket", "",
"the source and destination object names must both be non-empty",
},
{
"", "srcfile", "mybucket", "destname",
"the source and destination bucket names must both be non-empty",
},
{
"mybucket", "srcfile", "", "destname",
"the source and destination bucket names must both be non-empty",
},
}
ctx := context.Background()
client, err := NewClient(ctx, cloud.WithBaseHTTP(&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 := src.CopyTo(ctx, dst, nil)
if !strings.Contains(err.Error(), test.errMsg) {
t.Errorf("CopyTo test #%v:\ngot err %q\nwant err %q", i, err, test.errMsg)
}
}
}
开发者ID:trythings,项目名称:trythings,代码行数:36,代码来源:storage_test.go
示例17: TestObjects
//.........这里部分代码省略.........
// Test UpdateAttrs.
updated, err := bkt.Object(name).Update(ctx, ObjectAttrs{
ContentType: "text/html",
ACL: []ACLRule{{Entity: "domain-google.com", Role: RoleReader}},
})
if err != nil {
t.Errorf("UpdateAttrs failed with %v", err)
}
if want := "text/html"; updated.ContentType != want {
t.Errorf("updated.ContentType == %q; want %q", updated.ContentType, want)
}
// Test checksums.
checksumCases := []struct {
name string
contents [][]byte
size int64
md5 string
crc32c uint32
}{
{
name: "checksum-object",
contents: [][]byte{[]byte("hello"), []byte("world")},
size: 10,
md5: "fc5e038d38a57032085441e7fe7010b0",
crc32c: 1456190592,
},
{
name: "zero-object",
contents: [][]byte{},
size: 0,
md5: "d41d8cd98f00b204e9800998ecf8427e",
crc32c: 0,
},
}
for _, c := range checksumCases {
wc := bkt.Object(c.name).NewWriter(ctx)
for _, data := range c.contents {
if _, err := wc.Write(data); err != nil {
t.Errorf("Write(%q) failed with %q", data, err)
}
}
if err = wc.Close(); err != nil {
t.Errorf("%q: close failed with %q", c.name, err)
}
obj := wc.Attrs()
if got, want := obj.Size, c.size; got != want {
t.Errorf("Object (%q) Size = %v; want %v", c.name, got, want)
}
if got, want := fmt.Sprintf("%x", obj.MD5), c.md5; got != want {
t.Errorf("Object (%q) MD5 = %q; want %q", c.name, got, want)
}
if got, want := obj.CRC32C, c.crc32c; got != want {
t.Errorf("Object (%q) CRC32C = %v; want %v", c.name, got, want)
}
}
// Test public ACL.
publicObj := objects[0]
if err = bkt.Object(publicObj).ACL().Set(ctx, AllUsers, RoleReader); err != nil {
t.Errorf("PutACLEntry failed with %v", err)
}
publicClient, err := NewClient(ctx, cloud.WithBaseHTTP(http.DefaultClient))
if err != nil {
t.Error(err)
}
rc, err := publicClient.Bucket(bucket).Object(publicObj).NewReader(ctx)
if err != nil {
t.Error(err)
}
slurp, err := ioutil.ReadAll(rc)
if err != nil {
t.Errorf("ReadAll failed with %v", err)
}
if !bytes.Equal(slurp, contents[publicObj]) {
t.Errorf("Public object's content: got %q, want %q", slurp, contents[publicObj])
}
rc.Close()
// Test writer error handling.
wc := publicClient.Bucket(bucket).Object(publicObj).NewWriter(ctx)
if _, err := wc.Write([]byte("hello")); err != nil {
t.Errorf("Write unexpectedly failed with %v", err)
}
if err = wc.Close(); err == nil {
t.Error("Close expected an error, found none")
}
// DeleteObject object.
// The rest of the other object will be deleted during
// the initial cleanup. This tests exists, so we still can cover
// deletion if there are no objects on the bucket to clean.
if err := bkt.Object(copyObj).Delete(ctx); err != nil {
t.Errorf("Deletion of %v failed with %v", copyObj, err)
}
_, err = bkt.Object(copyObj).Attrs(ctx)
if err != ErrObjectNotExist {
t.Errorf("Copy is expected to be deleted, stat errored with %v", err)
}
}
开发者ID:stevearm,项目名称:camlistore,代码行数:101,代码来源:integration_test.go
示例18: uploadDockerImage
// uploadDockerImage makes a tar.gz snapshot of the camlistored docker image,
// and uploads it at camlistore-release/docker/camlistored-REV.tar.gz. It then
// makes a copy in the same bucket and path as camlistored.tar.gz.
func uploadDockerImage() {
proj := "camlistore-website"
bucket := "camlistore-release"
versionedTarball := "docker/camlistored-" + rev() + ".tar.gz"
tarball := "docker/camlistored.tar.gz"
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, cloud.WithTokenSource(ts), cloud.WithBaseHTTP(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?
w.ContentType = "application/x-gtar"
dockerSave := exec.Command("docker", "save", serverImage)
dockerSave.Stderr = os.Stderr
tar, err := dockerSave.StdoutPipe()
if err != nil {
log.Fatal(err)
}
targz, pw := io.Pipe()
go func() {
zw := gzip.NewWriter(pw)
n, err := io.Copy(zw, tar)
if err != nil {
log.Fatalf("Error copying to gzip writer: after %d bytes, %v", n, err)
}
if err := zw.Close(); err != nil {
log.Fatalf("gzip.Close: %v", err)
}
pw.CloseWithError(err)
}()
if err := dockerSave.Start(); err != nil {
log.Fatalf("Error starting docker save %v: %v", serverImage, err)
}
if _, err := io.Copy(w, targz); err != nil {
log.Fatalf("io.Copy: %v", err)
}
if err := w.Close(); err != nil {
log.Fatalf("closing GCS storage writer: %v", err)
}
if err := dockerSave.Wait(); err != nil {
log.Fatalf("Error waiting for docker save %v: %v", serverImage, err)
}
log.Printf("Uploaded tarball to %s", versionedTarball)
if !isWIP() {
log.Printf("Copying tarball to %s/%s ...", bucket, tarball)
if _, err := stoClient.CopyObject(ctx,
bucket, versionedTarball,
bucket, tarball,
&storage.ObjectAttrs{
ACL: publicACL(proj),
CacheControl: "no-cache",
ContentType: "application/x-gtar",
}); err != nil {
log.Fatalf("Error uploading %v: %v", tarball, err)
}
log.Printf("Uploaded tarball to %s", tarball)
}
}
开发者ID:rfistman,项目名称:camlistore,代码行数:71,代码来源:dock.go
示例19: TestRun
//.........这里部分代码省略.........
return &http.Response{
Body: ioutil.NopCloser(strings.NewReader("")),
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
StatusCode: http.StatusOK,
}, nil
}}
rt := &fakeTransport{func(r *http.Request) (resp *http.Response, e error) {
resp = &http.Response{
Body: ioutil.NopCloser(strings.NewReader(`{"name": "fake"}`)),
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
StatusCode: http.StatusOK,
}
// skip bucket.Attr() requests
if r.URL.Path == "/storage/v1/b/pr-123-suffix" && r.Method == "GET" {
return resp, nil
}
up := "/upload/storage/v1/b/pr-123-suffix/o"
if r.URL.Path != up {
t.Errorf("r.URL.Path = %q; want %q", r.URL.Path, up)
}
_, mp, err := mime.ParseMediaType(r.Header.Get("content-type"))
if err != nil {
t.Errorf("ParseMediaType: %v", err)
return
}
mr := multipart.NewReader(r.Body, mp["boundary"])
// metadata
p, err := mr.NextPart()
if err != nil {
t.Errorf("meta NextPart: %v", err)
return
}
var attrs storage.ObjectAttrs
if err := json.NewDecoder(p).Decode(&attrs); err != nil {
t.Errorf("meta json: %v", err)
return
}
seenMu.Lock()
seen[attrs.Name] = struct{}{}
seenMu.Unlock()
obj := files[attrs.Name]
if obj == nil {
t.Errorf("unexpected obj: %+v", attrs)
return
}
if attrs.Bucket != "pr-123-suffix" {
t.Errorf("attrs.Bucket = %q; want pr-123-suffix", attrs.Bucket)
}
if attrs.CacheControl != vargs.CacheControl {
t.Errorf("attrs.CacheControl = %q; want %q", attrs.CacheControl, vargs.CacheControl)
}
if obj.gzip && attrs.ContentEncoding != "gzip" {
t.Errorf("attrs.ContentEncoding = %q; want gzip", attrs.ContentEncoding)
}
if !strings.HasPrefix(attrs.ContentType, obj.ctype) {
t.Errorf("attrs.ContentType = %q; want %q", attrs.ContentType, obj.ctype)
}
if !reflect.DeepEqual(attrs.ACL, acls) {
t.Errorf("attrs.ACL = %v; want %v", attrs.ACL, acls)
}
if !reflect.DeepEqual(attrs.Metadata, vargs.Metadata) {
t.Errorf("attrs.Metadata = %+v; want %+v", attrs.Metadata, vargs.Metadata)
}
// media
p, err = mr.NextPart()
if err != nil {
t.Errorf("media NextPart: %v", err)
return
}
b, _ := ioutil.ReadAll(p)
if attrs.ContentEncoding == "gzip" {
b = gunzip(t, b)
}
if bytes.Compare(b, obj.body) != 0 {
t.Errorf("media b = %q; want %q", b, obj.body)
}
return
}}
client.http = &http.Client{Transport: okTransport}
client.ghub = github.NewClient(&http.Client{Transport: okTransport})
hc := &http.Client{Transport: rt}
if client.gcs, err = storage.NewClient(context.Background(), cloud.WithBaseHTTP(hc)); err != nil {
t.Fatal(err)
}
run()
for k := range files {
if _, ok := seen[k]; !ok {
t.Errorf("%s didn't get uploaded", k)
}
}
}
开发者ID:peterwang1996,项目名称:WebFundamentals,代码行数:101,代码来源:main_test.go
示例20: listDownloads
// listDownloads lists all the files found in the monthly repo, and from them,
// builds the data that we'll feed to the template to generate the monthly
// downloads camweb page.
func listDownloads() (*ReleaseData, error) {
ts, err := tokenSource(bucket)
if err != nil {
return nil, err
}
ctx := context.Background()
stoClient, err := storage.NewClient(ctx, cloud.WithTokenSource(ts), cloud.WithBaseHTTP(oauth2.NewClient(ctx, ts)))
if err != nil {
return nil, err
}
objList, err := stoClient.Bucket(bucket).List(ctx, &storage.Query{Prefix: "monthly/"})
if err != nil {
return nil, err
}
platformBySuffix := map[string]string{
"src.zip": "Source",
"linux.tar.gz": "Linux",
"darwin.tar.gz": "Darwin",
"windows.zip": "Windows",
}
getPlatform := func(name string) string {
for suffix, platform := range platformBySuffix {
if strings.HasSuffix(name, suffix) {
return platform
}
}
return ""
}
getChecksum := func(name string) (string, error) {
r, err := stoClient.Bucket(bucket).Object(name).NewReader(ctx)
if err != nil {
return "", err
}
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return "", err
}
return buf.String(), nil
}
var date time.Time
checkDate := func(objDate time.Time) error {
if date.IsZero() {
date = objDate
return nil
}
d := date.Sub(objDate)
if d < 0 {
d = -d
}
if d < 24*time.Hour {
return nil
}
return fmt.Errorf("objects in monthly have not been uploaded or updated the same day")
}
var (
downloadData []DownloadData
nameToSum = make(map[string]string)
)
for _, attrs := range objList.Results {
if !strings.Contains(attrs.Name, rev()) {
continue
}
if err := checkDate(attrs.Updated); err != nil {
return nil, err
}
if !strings.HasSuffix(attrs.Name, ".sha256") {
continue
}
sum, err := getChecksum(attrs.Name)
if err != nil {
return nil, err
}
nameToSum[strings.TrimSuffix(attrs.Name, ".sha256")] = sum
}
for _, attrs := range objList.Results {
if !strings.Contains(attrs.Name, rev()) {
continue
}
if strings.HasSuffix(attrs.Name, ".sha256") {
continue
}
sum, ok := nameToSum[attrs.Name]
if !ok {
return nil, fmt.Errorf("%v has no checksum file!", attrs.Name)
}
downloadData = append(downloadData, DownloadData{
Filename: filepath.Base(attrs.Name),
Platform: getPlatform(attrs.Name),
Checksum: sum,
})
}
return &ReleaseData{
Date: date.Format("2006-01-02"),
Download: downloadData,
//.........这里部分代码省略.........
开发者ID:pombredanne,项目名称:camlistore,代码行数:101,代码来源:monthly.go
注:本文中的google/golang.org/cloud.WithBaseHTTP函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论