diff options
author | bojie <bojie@dexon.org> | 2018-12-28 13:15:06 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-03-12 12:19:09 +0800 |
commit | 487f038bf1cf303704154927f4de180b22a31f67 (patch) | |
tree | a609020f3a420a68deee099a8c86885a3260c873 /internal | |
parent | b8885b8345bab5e076fc72a0661a0d9a1b5cc421 (diff) | |
download | dexon-487f038bf1cf303704154927f4de180b22a31f67.tar dexon-487f038bf1cf303704154927f4de180b22a31f67.tar.gz dexon-487f038bf1cf303704154927f4de180b22a31f67.tar.bz2 dexon-487f038bf1cf303704154927f4de180b22a31f67.tar.lz dexon-487f038bf1cf303704154927f4de180b22a31f67.tar.xz dexon-487f038bf1cf303704154927f4de180b22a31f67.tar.zst dexon-487f038bf1cf303704154927f4de180b22a31f67.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 +} |