func (b *Build) Upload(version string, filename string) error { file, err := ioutil.ReadFile(filename) if err != nil { return err } svc, err := storage.New(oauthClient) if err != nil { return err } obj := &storage.Object{ Acl: []*storage.ObjectAccessControl{{Entity: "allUsers", Role: "READER"}}, Name: filename, } _, err = svc.Objects.Insert(*storageBucket, obj).Media(bytes.NewReader(file)).Do() if err != nil { return err } sum := fmt.Sprintf("%x", sha1.Sum(file)) kind := "unknown" switch { case b.Source: kind = "source" case strings.HasSuffix(filename, ".tar.gz"), strings.HasSuffix(filename, ".zip"): kind = "archive" case strings.HasSuffix(filename, ".msi"), strings.HasSuffix(filename, ".pkg"): kind = "installer" } req, err := json.Marshal(File{ Filename: filename, Version: version, OS: b.OS, Arch: b.Arch, Checksum: sum, Kind: kind, }) if err != nil { return err } u := fmt.Sprintf("%s?%s", *uploadURL, url.Values{"key": []string{builderKey}}.Encode()) resp, err := http.Post(u, "application/json", bytes.NewReader(req)) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return fmt.Errorf("upload status: %v", resp.Status) } return nil }
func (s *Gcs) getService(oauth_client *http.Client) *gcs.Service { if oauth_client == nil { log.Printf("Gcs Client not available\n") return nil } service, err := gcs.New(oauth_client) if err != nil { log.Printf("Gcs Service not available\n") return nil } return service }
func (s *Gcs) Delete(filename string, ext string) bool { log.Printf("Gcs delete in progress...\n") oauth_client := s.getOauthClient() service, err := gcs.New(oauth_client) if err != nil { log.Printf("Gcs Service not available\n") return false } err = service.Objects.Delete(s.Bucket, s.Directory+filename+"."+ext).Do() if err != nil { log.Printf("Gcs object deletion error: %v\n", err) } else { log.Printf("Gcs object deletion success\n") } return true }
func main() { goptions.ParseAndFail(&options) defer options.KeyFile.Close() pemBytes, err := ioutil.ReadAll(options.KeyFile) if err != nil { log.Fatalf("Could not read keyfile: %s", err) } token := jwt.NewToken(options.ClientId, storage.DevstorageRead_writeScope, pemBytes) // token.ClaimSet.Aud = aud c := &http.Client{} oauthToken, err := token.Assert(c) if err != nil { log.Fatalf("Could not get OAuth token: %s", err) } c.Transport = &oauth.Transport{ Token: oauthToken, } service, err := storage.New(c) if err != nil { log.Fatalf("Could not use storage API: %s", err) } objs, err := service.Objects.List(options.Bucket).Do() if err != nil { log.Fatalf("Could not list content of bucket %s: %s", options.Bucket, err) } for _, obj := range objs.Items { log.Printf("%s/%s: %s\n", options.Bucket, obj.Name, obj.SelfLink) } data := strings.NewReader("Some Data") newObj, err := service.Objects.Insert(options.Bucket, &storage.Object{}).Name("gcs-test").Media(data).Do() if err != nil { log.Fatalf("Could not create new object: %s", err) } log.Printf("Uploaded to %s", newObj.SelfLink) }