forked from kelseyhightower/gcscache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
66 lines (54 loc) · 1.72 KB
/
cache.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
// Copyright 2017 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// Package gcscache provides storage, backed by Google Cloud Storage,
// for certificates managed by the golang.org/x/crypto/acme/autocert package.
//
// This package is a work in progress and makes no API stability promises.
package gcscache
import (
"context"
"io/ioutil"
"cloud.google.com/go/storage"
"golang.org/x/crypto/acme/autocert"
)
// Cache implements the autocert.Cache interface using Google Cloud Storage.
type Cache struct {
client *storage.Client
bucket string
}
func New(bucket string) (*Cache, error) {
client, err := storage.NewClient(context.Background())
if err != nil {
return nil, err
}
c := &Cache{client, bucket}
return c, nil
}
// Get reads a certificate data from the specified object name.
func (c *Cache) Get(ctx context.Context, name string) ([]byte, error) {
r, err := c.client.Bucket(c.bucket).Object(name).NewReader(context.Background())
if err == storage.ErrObjectNotExist {
return nil, autocert.ErrCacheMiss
}
if err != nil {
return nil, err
}
defer r.Close()
return ioutil.ReadAll(r)
}
// Put writes the certificate data to the specified object name.
func (c *Cache) Put(ctx context.Context, name string, data []byte) error {
w := c.client.Bucket(c.bucket).Object(name).NewWriter(context.Background())
w.Write(data)
return w.Close()
}
// Delete removes the specified object name.
func (c *Cache) Delete(ctx context.Context, name string) error {
o := c.client.Bucket(c.bucket).Object(name)
err := o.Delete(context.Background())
if err == storage.ErrObjectNotExist {
return nil
}
return err
}