/
gcs.go
67 lines (54 loc) · 1.57 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
package cacher
import (
"context"
"hash"
"sync"
"github.com/goproxy/goproxy"
)
// GCS implements the `goproxy.Cacher` by using the Google Cloud Storage.
type GCS struct {
// Endpoint is the endpoint of the Google Cloud Storage.
//
// If the `Endpoint` is empty, the "https://storage.googleapis.com" is
// used.
Endpoint string `mapstructure:"endpoint"`
// AccessKey is the access key of the Google Cloud Platform.
AccessKey string `mapstructure:"access_key"`
// SecretKey is the secret key of the Google Cloud Platform.
SecretKey string `mapstructure:"secret_key"`
// BucketName is the name of the bucket.
BucketName string `mapstructure:"bucket_name"`
// Root is the root of the caches.
Root string `mapstructure:"root"`
loadOnce sync.Once
minio *MinIO
}
// load loads the stuff of the m up.
func (g *GCS) load() {
endpoint := g.Endpoint
if endpoint == "" {
endpoint = "https://storage.googleapis.com"
}
g.minio = &MinIO{
Endpoint: endpoint,
AccessKeyID: g.AccessKey,
SecretAccessKey: g.SecretKey,
BucketName: g.BucketName,
VirtualHosted: true,
Root: g.Root,
}
}
// NewHash implements the `goproxy.Cacher`.
func (g *GCS) NewHash() hash.Hash {
return g.minio.NewHash()
}
// Cache implements the `goproxy.Cacher`.
func (g *GCS) Cache(ctx context.Context, name string) (goproxy.Cache, error) {
g.loadOnce.Do(g.load)
return g.minio.Cache(ctx, name)
}
// SetCache implements the `goproxy.Cacher`.
func (g *GCS) SetCache(ctx context.Context, c goproxy.Cache) error {
g.loadOnce.Do(g.load)
return g.minio.SetCache(ctx, c)
}