-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathgcs.go
124 lines (109 loc) · 3.19 KB
/
gcs.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package gcloud
import (
"context"
"errors"
"fmt"
"net/url"
"strings"
"cloud.google.com/go/storage"
"github.com/Shopify/go-lua"
"github.com/treeverse/lakefs/pkg/actions/lua/path"
"github.com/treeverse/lakefs/pkg/actions/lua/util"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)
const (
// googleAuthCloudPlatform - Cloud Storage authentication https://cloud.google.com/storage/docs/authentication
googleAuthCloudPlatform = "https://www.googleapis.com/auth/cloud-platform"
)
var ErrInvalidGCSURI = errors.New("invalid Google Cloud Storage URI")
func Open(l *lua.State, ctx context.Context) {
open := func(l *lua.State) int {
lua.NewLibrary(l, []lua.RegistryFunction{
{Name: "gs_client", Function: newGSClient(ctx)},
})
return 1
}
lua.Require(l, "gcloud", open, false)
l.Pop(1)
}
func newGSClient(ctx context.Context) lua.Function {
return func(l *lua.State) int {
json := lua.CheckString(l, 1)
c := &GSClient{
JSON: json,
ctx: ctx,
}
l.NewTable()
for name, goFn := range functions {
// -1: tbl
l.PushGoFunction(goFn(c))
// -1: fn, -2:tbl
l.SetField(-2, name)
}
return 1
}
}
type GSClient struct {
JSON string
ctx context.Context
}
func (c *GSClient) client() (*storage.Client, error) {
cred, err := google.CredentialsFromJSON(c.ctx, []byte(c.JSON), googleAuthCloudPlatform)
if err != nil {
return nil, err
}
return storage.NewClient(c.ctx, option.WithCredentials(cred))
}
var functions = map[string]func(client *GSClient) lua.Function{
"write_fuse_symlink": writeFuseSymlink,
}
func writeFuseSymlink(c *GSClient) lua.Function {
return func(l *lua.State) int {
// convert the relative physical address with the mount point
physicalAddress := lua.CheckString(l, 1)
outputAddress := lua.CheckString(l, 2)
mountInfo, err := util.PullStringTable(l, 3)
if err != nil {
lua.Errorf(l, "could not read mount info: %s", err.Error())
}
// let's resolve the path:
if fromValue, removeFrom := mountInfo["from"]; removeFrom {
physicalAddress = strings.TrimPrefix(physicalAddress, fromValue)
}
if toValue, prependTo := mountInfo["to"]; prependTo {
physicalAddress = path.Join("/", toValue, physicalAddress)
}
// write the object
client, err := c.client()
if err != nil {
lua.Errorf(l, "could not initialize google storage client: %s", err.Error())
}
defer func() { _ = client.Close() }()
obj, err := asObject(client, outputAddress)
if err != nil {
lua.Errorf(l, "could not parse destination object \"%s\": %s", outputAddress, err.Error())
}
w := obj.NewWriter(c.ctx)
w.ObjectAttrs.Metadata = map[string]string{
"gcsfuse_symlink_target": physicalAddress,
}
err = w.Close()
if err != nil {
lua.Errorf(l, "could not close object \"%s\": %s", outputAddress, err.Error())
}
return 0
}
}
func asObject(client *storage.Client, gsURI string) (*storage.ObjectHandle, error) {
parsed, err := url.Parse(gsURI)
if err != nil {
return nil, fmt.Errorf("unable to parse URI: %s: %w", gsURI, ErrInvalidGCSURI)
}
if parsed.Scheme != "gs" {
return nil, ErrInvalidGCSURI
}
bucket := parsed.Host
objectPath := strings.TrimPrefix(parsed.Path, path.SEPARATOR)
return client.Bucket(bucket).Object(objectPath), nil
}