This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_blobstore.go
115 lines (92 loc) · 2.42 KB
/
s3_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
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
package releasedir
import (
gobytes "bytes"
"encoding/json"
"os"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
s3client "github.com/pivotal-golang/s3cli/client"
s3config "github.com/pivotal-golang/s3cli/config"
)
type S3Blobstore struct {
fs boshsys.FileSystem
uuidGen boshuuid.Generator
options map[string]interface{}
}
func NewS3Blobstore(
fs boshsys.FileSystem,
uuidGen boshuuid.Generator,
options map[string]interface{},
) S3Blobstore {
return S3Blobstore{
fs: fs,
uuidGen: uuidGen,
options: options,
}
}
func (b S3Blobstore) Get(blobID string) (string, error) {
client, err := b.client()
if err != nil {
return "", err
}
file, err := b.fs.TempFile("bosh-s3-blob")
if err != nil {
return "", bosherr.WrapError(err, "Creating destination file")
}
defer file.Close()
err = client.Get(blobID, file)
if err != nil {
return "", err
}
return file.Name(), nil
}
func (b S3Blobstore) Create(path string) (string, error) {
client, err := b.client()
if err != nil {
return "", err
}
blobID, err := b.uuidGen.Generate()
if err != nil {
return "", bosherr.WrapError(err, "Generating blobstore ID")
}
file, err := b.fs.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
return "", bosherr.WrapError(err, "Opening source file")
}
defer file.Close()
err = client.Put(file, blobID)
if err != nil {
return "", bosherr.WrapError(err, "Generating blobstore ID")
}
return blobID, nil
}
func (b S3Blobstore) CleanUp(path string) error {
return b.fs.RemoveAll(path)
}
func (b S3Blobstore) Delete(blobID string) error {
panic("Not implemented")
}
func (b S3Blobstore) Validate() error {
_, err := b.client()
return err
}
func (b S3Blobstore) client() (s3client.S3Blobstore, error) {
bytes, err := json.Marshal(b.options)
if err != nil {
return s3client.S3Blobstore{}, bosherr.WrapErrorf(err, "Marshaling config")
}
conf, err := s3config.NewFromReader(gobytes.NewBuffer(bytes))
if err != nil {
return s3client.S3Blobstore{}, bosherr.WrapErrorf(err, "Reading config")
}
s3ClientSDK, err := s3client.NewSDK(conf)
if err != nil {
return s3client.S3Blobstore{}, bosherr.WrapErrorf(err, "Building client SDK")
}
client, err := s3client.New(s3ClientSDK, &conf)
if err != nil {
return s3client.S3Blobstore{}, bosherr.WrapErrorf(err, "Validating config")
}
return client, nil
}