-
Notifications
You must be signed in to change notification settings - Fork 115
/
blobstore.go
67 lines (56 loc) · 1.31 KB
/
blobstore.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 utils
import (
"io"
"net/http"
"os"
davclient "github.com/cloudfoundry/bosh-davcli/client"
davconfig "github.com/cloudfoundry/bosh-davcli/config"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
"github.com/cloudfoundry/bosh-utils/uuid"
)
type BlobClient struct {
dav davclient.Client
uuidGenerator uuid.Generator
}
func (b BlobClient) Create(filepath string) (string, error) {
uuid, err := b.uuidGenerator.Generate()
if err != nil {
return "", err
}
file, err := os.Open(filepath)
if err != nil {
return "", err
}
info, err := file.Stat()
if err != nil {
return "", err
}
err = b.dav.Put(uuid, file, info.Size())
if err != nil {
return "", err
}
return uuid, nil
}
func (b BlobClient) Get(uuid string, destinationPath string) error {
readCloser, err := b.dav.Get(uuid)
if err != nil {
return err
}
defer readCloser.Close()
targetFile, err := os.Create(destinationPath)
if err != nil {
return err
}
defer targetFile.Close()
_, err = io.Copy(targetFile, readCloser)
return err
}
func NewBlobstore(uri string) BlobClient {
config := davconfig.Config{Endpoint: uri}
logger := boshlog.NewLogger(boshlog.LevelNone)
client := davclient.NewClient(config, http.DefaultClient, logger)
return BlobClient{
dav: client,
uuidGenerator: uuid.NewGenerator(),
}
}