-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (81 loc) · 3.19 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
87
88
//
// Copyright 2023 Google. This software is provided as-is, without warranty
// or representation for any use or purpose. Your use of it is subject
// to your agreement with Google.
//
package main
import (
"context"
"fmt"
"io"
"time"
"os"
"log"
"bufio"
"cloud.google.com/go/storage"
"google.golang.org/api/impersonate"
"google.golang.org/api/option"
)
// generateV4GetObjectSignedURL generates object signed URL with PUT method.
func generateV4PutObjectSignedURL(w io.Writer, bucket, object string) (string, error) {
// bucket := "bucket-name"
// object := "object-name"
ctx := context.Background()
// impersonate service accouhnt
// Base credentials sourced from ADC or provided client options.
ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
TargetPrincipal: "signurl-service-account@ashires-joonix-test-1.iam.gserviceaccount.com",
Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
// Optionally supply delegates.
// Delegates: []string{"bar@project-id.iam.gserviceaccount.com"},
})
if err != nil {
log.Fatal(err)
}
// get storage client using impersonated credentaiisl
client, err := storage.NewClient(ctx, option.WithTokenSource(ts))
if err != nil {
return "", fmt.Errorf("storage.NewClient: %v", err)
}
defer client.Close()
// Signing a URL requires credentials authorized to sign a URL. You can pass
// these in through SignedURLOptions with one of the following options:
// a. a Google service account private key, obtainable from the Google Developers Console
// b. a Google Access ID with iam.serviceAccounts.signBlob permissions
// c. a SignBytes function implementing custom signing.
// In this example, none of these options are used, which means the SignedURL
// function attempts to use the same authentication that was used to instantiate
// the Storage client. This authentication must include a private key or have
// iam.serviceAccounts.signBlob permissions.
opts := &storage.SignedURLOptions{
Scheme: storage.SigningSchemeV4,
Method: "PUT",
Headers: []string{
"Content-Type:application/octet-stream",
},
Expires: time.Now().Add(12 * time.Hour),
GoogleAccessID: "signurl-service-account@ashires-joonix-test-1.iam.gserviceaccount.com",
}
u, err := client.Bucket(bucket).SignedURL(object, opts)
if err != nil {
return "", fmt.Errorf("Bucket(%q).SignedURL: %v", bucket, err)
}
fmt.Printf("Generated PUT signed URL:")
fmt.Printf("%q\n", u)
fmt.Printf("You can use this URL with any user agent, for example:")
fmt.Printf("curl -X PUT -H 'Content-Type: application/octet-stream' --upload-file my-file %q\n", u)
return u, nil
}
func main() {
fmt.Println("help text goes here")
fmt.Printf("args: %s", os.Args)
var gcs_bucket = os.Args[1]
var gcs_object = os.Args[2]
var out = os.Stdout
var writer = bufio.NewWriter(out)
var signed_url, err = generateV4PutObjectSignedURL(writer, gcs_bucket, gcs_object)
if err != nil {
fmt.Println(signed_url)
}
fmt.Println(err)
}