-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.go
66 lines (53 loc) · 1.54 KB
/
storage.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
// Provides functionalities to use GCP Cloud Storage as remote model store
package cloudstorage
import (
"context"
"io"
"os"
"cloud.google.com/go/storage"
"github.com/DHBWMannheim/ml-server/util"
"google.golang.org/api/option"
)
type Storage interface {
// Downloads the model based on the full path provided
// in the `name` parameter.
//
// It returns the path, where the models lives. In case
// the model was not present, it returns an storage.ErrObjNotFound.
DownloadModel(ctx context.Context, name, modelPath string) (string, error)
}
type cloudStorage struct {
bucket *storage.BucketHandle
}
func NewCloudStorageService(ctx context.Context, bucketName string) Storage {
client, err := storage.NewClient(ctx, option.WithoutAuthentication(), option.WithScopes(storage.ScopeReadOnly))
if err != nil {
panic(err)
}
c := &cloudStorage{}
c.bucket = client.Bucket(bucketName)
return c
}
// Downloads the model based on the full path provided
// in the `name` parameter.
//
// It returns the path, where the models lives. In case
// the model was not present, it returns an storage.ErrObjNotFound.
func (c *cloudStorage) DownloadModel(ctx context.Context, name, modelPath string) (string, error) {
rc, err := c.bucket.Object(name).NewReader(ctx)
if err != nil {
return "", err
}
defer rc.Close()
f, err := os.CreateTemp("", "*.zip")
defer os.Remove(f.Name())
if err != nil {
return "", err
}
defer f.Close()
io.Copy(f, rc)
if err := util.ExtractTfArchive(f, modelPath); err != nil {
return "", err
}
return modelPath, nil
}