-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
86 lines (69 loc) · 1.7 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
object string
fileName string
)
flag.StringVar(&bucket, "b", "", "Bucket name")
flag.StringVar(&keyFile, "k", "", "GCP key.json file")
flag.StringVar(&object, "o", "", "object to download")
flag.StringVar(&fileName, "f", "", "filename to call object")
flag.Parse()
if bucket == "" {
fmt.Println("Bucket Required")
os.Exit(1)
}
if keyFile == "" {
fmt.Println("GCP key.json required")
os.Exit(1)
}
if fileName == "" {
fmt.Println("Filename required")
os.Exit(1)
}
err := downloadFile(os.Stdout, bucket, object, fileName, keyFile)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
}
// downloadFile downloads an object to a file.
func downloadFile(w io.Writer, bucket, object string, destFileName, jsonPath string) error {
ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithCredentialsFile(jsonPath))
if err != nil {
return fmt.Errorf("storage.NewClient: %v", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()
f, err := os.Create(destFileName)
if err != nil {
return fmt.Errorf("os.Create: %v", err)
}
rc, err := client.Bucket(bucket).Object(object).NewReader(ctx)
if err != nil {
return fmt.Errorf("Object(%q).NewReader: %v", object, err)
}
defer rc.Close()
if _, err := io.Copy(f, rc); err != nil {
return fmt.Errorf("io.Copy: %v", err)
}
if err = f.Close(); err != nil {
return fmt.Errorf("f.Close: %v", err)
}
fmt.Fprintf(w, "Blob %v downloaded to local file %v\n", object, destFileName)
return nil
}