-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (43 loc) · 1 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
package main
import (
"context"
"flag"
"fmt"
"log"
"regexp"
"time"
"cloud.google.com/go/storage"
"github.com/apstndb/signer"
)
func main() {
if err := _main(); err != nil {
log.Fatalln(err)
}
}
func _main() error {
method := flag.String("m", "GET", "")
duration := flag.Duration("d", 1*time.Hour, "")
flag.Parse()
ctx := context.Background()
s, err := signer.SmartSigner(ctx)
if err != nil {
return err
}
re := regexp.MustCompile(`^gs://(?P<bucket>[^/]*)/(?P<name>.*)$`)
fmt.Println(flag.Args()[0])
submatch := re.FindStringSubmatch(flag.Args()[0])
bucket := submatch[re.SubexpIndex("bucket")]
name := submatch[re.SubexpIndex("name")]
fmt.Printf("bucket: %s, name: %s\n", bucket, name)
url, err := storage.SignedURL(bucket, name, &storage.SignedURLOptions{
GoogleAccessID: s.ServiceAccount(ctx),
SignBytes: signer.SignWithoutKeyAdaptor(ctx, s),
Method: *method,
Expires: time.Now().Add(*duration),
})
if err != nil {
return err
}
fmt.Println(url)
return nil
}