-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (66 loc) · 1.94 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
package main
import (
"context"
"fmt"
"io"
"os"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/pilagod/docker-postgres-command/pkg"
)
func main() {
pgDumpPath, err := pkg.Dump(
pkg.DumpOption{
Connection: pkg.Connection{
Host: os.Getenv("HOST"),
Port: os.Getenv("PORT"),
DB: os.Getenv("DB"),
Username: os.Getenv("USERNAME"),
Password: os.Getenv("PASSWORD"),
},
Flags: os.Getenv("DUMP_FLAGS"),
},
)
if err != nil {
panic(err)
}
defer os.Remove(pgDumpPath)
bucket := os.Getenv("BUCKET")
object := pgDumpPath[strings.LastIndex(pgDumpPath, "/")+1:]
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
panic(fmt.Errorf("storage.NewClient: %v", err))
}
defer client.Close()
pgDumpFile, err := os.Open(pgDumpPath)
if err != nil {
panic(fmt.Errorf("os.Open: %v", err))
}
defer pgDumpFile.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*60)
defer cancel()
o := client.Bucket(bucket).Object(object)
// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload is aborted if the
// object's generation number does not match your precondition.
// For an object that does not yet exist, set the DoesNotExist precondition.
o = o.If(storage.Conditions{DoesNotExist: true})
// If the live object already exists in your bucket, set instead a
// generation-match precondition using the live object's generation number.
// attrs, err := o.Attrs(ctx)
// if err != nil {
// return fmt.Errorf("object.Attrs: %v", err)
// }
// o = o.If(storage.Conditions{GenerationMatch: attrs.Generation})
// Upload an object with storage.Writer.
wc := o.NewWriter(ctx)
if _, err := io.Copy(wc, pgDumpFile); err != nil {
panic(fmt.Errorf("io.Copy: %v", err))
}
if err := wc.Close(); err != nil {
panic(fmt.Errorf("Writer.Close: %v", err))
}
fmt.Printf("Blob %v uploaded.\n", object)
}