-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (72 loc) · 1.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/option"
)
func main() {
var (
bucket string
keyFile string
file string
)
flag.StringVar(&bucket, "b", "", "Bucket name")
flag.StringVar(&keyFile, "k", "", "GCP key.json file")
flag.StringVar(&file, "f", "", "File to upload, will be named the same in the bucket")
flag.Parse()
if bucket == "" {
fmt.Println("Bucket Required")
os.Exit(1)
}
if file == "" {
fmt.Println("File to upload required")
os.Exit(1)
}
err := uploadFile(os.Stdout, bucket, file, keyFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func uploadFile(w io.Writer, bucket, object, jsonPath string) error {
ctx := context.Background()
var client *storage.Client
var err error
if jsonPath == "" {
client, err = storage.NewClient(ctx)
} else {
client, err = storage.NewClient(ctx, option.WithCredentialsFile(jsonPath))
}
if err != nil {
return fmt.Errorf("storage.NewClient: %v", err)
}
defer client.Close()
// Open local file.
f, err := os.Open(object)
if err != nil {
return fmt.Errorf("os.Open: %v", err)
}
defer f.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()
// Upload an object with storage.Writer.
wc := client.Bucket(bucket).Object(object).NewWriter(ctx)
wc.ProgressFunc = progressfunc
if _, err = io.Copy(wc, f); err != nil {
return fmt.Errorf("io.Copy: %v", err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("Writer.Close: %v", err)
}
fmt.Fprintf(w, "Blob %v uploaded.\n", object)
return nil
}
func progressfunc(copiedBytes int64) {
mb := copiedBytes / 1024 / 1024
fmt.Printf("%dMB Uploaded\n", mb)
}