diff options
author | bojie <bojie@dexon.org> | 2018-12-28 13:15:06 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:27:20 +0800 |
commit | 3d46f69a10a60324ee6af0c96d064cc73ed5c37d (patch) | |
tree | ae9dd76f3f9a69c0acb18f6b0343601558d2e00f /internal | |
parent | 2426b985ae9773afe12f5a9f982cf4f2629f81a3 (diff) | |
download | go-tangerine-3d46f69a10a60324ee6af0c96d064cc73ed5c37d.tar go-tangerine-3d46f69a10a60324ee6af0c96d064cc73ed5c37d.tar.gz go-tangerine-3d46f69a10a60324ee6af0c96d064cc73ed5c37d.tar.bz2 go-tangerine-3d46f69a10a60324ee6af0c96d064cc73ed5c37d.tar.lz go-tangerine-3d46f69a10a60324ee6af0c96d064cc73ed5c37d.tar.xz go-tangerine-3d46f69a10a60324ee6af0c96d064cc73ed5c37d.tar.zst go-tangerine-3d46f69a10a60324ee6af0c96d064cc73ed5c37d.zip |
app: use gcp storage instead of azure for builds (#81)
* vendor: add dependencies for using GCP storage
* app: use gcp storage instead of azure for builds
Diffstat (limited to 'internal')
-rw-r--r-- | internal/build/gcp.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/internal/build/gcp.go b/internal/build/gcp.go new file mode 100644 index 000000000..3c312a341 --- /dev/null +++ b/internal/build/gcp.go @@ -0,0 +1,82 @@ +package build + +import ( + "io" + "os" + + "cloud.google.com/go/storage" + "golang.org/x/net/context" + "google.golang.org/api/iterator" + "google.golang.org/api/option" +) + +type GCPOption struct { + CredentialPath string +} + +// GCPFileUpload upload file to GCP storage +func GCPFileUpload(path, bucket, name string, opt GCPOption) error { + ctx := context.Background() + client, err := storage.NewClient(ctx, option.WithCredentialsFile(opt.CredentialPath)) + if err != nil { + return err + } + + wc := client.Bucket(bucket).Object(name).NewWriter(ctx) + + in, err := os.Open(path) + if err != nil { + return err + } + defer in.Close() + + if _, err = io.Copy(wc, in); err != nil { + return err + } + if err := wc.Close(); err != nil { + return err + } + + return nil +} + +// GCPFileList list files from GCP storage +func GCPFileList(bucket string, opt GCPOption) ([]*storage.ObjectAttrs, error) { + ctx := context.Background() + client, err := storage.NewClient(ctx, option.WithCredentialsFile(opt.CredentialPath)) + if err != nil { + return nil, err + } + + var list []*storage.ObjectAttrs + it := client.Bucket(bucket).Objects(ctx, nil) + for { + attrs, err := it.Next() + if err == iterator.Done { + break + } + if err != nil { + return nil, err + } + list = append(list, attrs) + } + + return list, nil +} + +// GCPFileDelete delete files from GCP storage +func GCPFileDelete(bucket string, objects []*storage.ObjectAttrs, opt GCPOption) error { + ctx := context.Background() + client, err := storage.NewClient(ctx, option.WithCredentialsFile(opt.CredentialPath)) + if err != nil { + return err + } + + for _, obj := range objects { + if err := client.Bucket(bucket).Object(obj.Name).Delete(ctx); err != nil { + return err + } + } + + return nil +} |