-
Notifications
You must be signed in to change notification settings - Fork 392
/
uplink.go
145 lines (120 loc) · 3.49 KB
/
uplink.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package s3client
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/zeebo/errs"
"storj.io/common/fpath"
)
// UplinkError is class for minio errors
var UplinkError = errs.Class("uplink error")
// Uplink implements basic S3 Client with uplink
type Uplink struct {
conf Config
}
// NewUplink creates new Client
func NewUplink(conf Config) (Client, error) {
client := &Uplink{conf}
if client.conf.ConfigDir != "" {
fmt.Printf("Using existing uplink config at %s\n", client.conf.ConfigDir)
return client, nil
}
client.conf.ConfigDir = fpath.ApplicationDir("storj", "s3-client", "uplink")
// remove existing s3client uplink config so that
// we can create a new one with up-to-date settings
err := os.RemoveAll(client.conf.ConfigDir)
if err != nil {
return nil, UplinkError.Wrap(fullExitError(err))
}
cmd := client.cmd("--config-dir", client.conf.ConfigDir,
"setup",
"--non-interactive", "true",
"--api-key", client.conf.APIKey,
"--enc.encryption-key", client.conf.EncryptionKey,
"--satellite-addr", client.conf.Satellite,
)
_, err = cmd.Output()
if err != nil {
return nil, UplinkError.Wrap(fullExitError(err))
}
return client, nil
}
func (client *Uplink) cmd(subargs ...string) *exec.Cmd {
args := []string{}
configArgs := []string{"--config-dir", client.conf.ConfigDir}
args = append(args, configArgs...)
args = append(args, subargs...)
cmd := exec.Command("uplink", args...)
return cmd
}
// MakeBucket makes a new bucket
func (client *Uplink) MakeBucket(bucket, location string) error {
cmd := client.cmd("mb", "s3://"+bucket)
_, err := cmd.Output()
if err != nil {
return UplinkError.Wrap(fullExitError(err))
}
return nil
}
// RemoveBucket removes a bucket
func (client *Uplink) RemoveBucket(bucket string) error {
cmd := client.cmd("rb", "s3://"+bucket)
_, err := cmd.Output()
if err != nil {
return UplinkError.Wrap(fullExitError(err))
}
return nil
}
// ListBuckets lists all buckets
func (client *Uplink) ListBuckets() ([]string, error) {
cmd := client.cmd("ls")
data, err := cmd.Output()
if err != nil {
return nil, UplinkError.Wrap(fullExitError(err))
}
names := strings.Split(strings.TrimRight(string(data), "\n"), "\n")
return names, nil
}
// Upload uploads object data to the specified path
func (client *Uplink) Upload(bucket, objectName string, data []byte) error {
// TODO: add upload threshold
cmd := client.cmd("put", "s3://"+bucket+"/"+objectName)
cmd.Stdin = bytes.NewReader(data)
_, err := cmd.Output()
if err != nil {
return UplinkError.Wrap(fullExitError(err))
}
return nil
}
// Download downloads object data
func (client *Uplink) Download(bucket, objectName string, buffer []byte) ([]byte, error) {
cmd := client.cmd("cat", "s3://"+bucket+"/"+objectName)
out, err := cmd.Output()
if err != nil {
return nil, UplinkError.Wrap(fullExitError(err))
}
return out, nil
}
// Delete deletes object
func (client *Uplink) Delete(bucket, objectName string) error {
cmd := client.cmd("rm", "s3://"+bucket+"/"+objectName)
_, err := cmd.Output()
if err != nil {
return UplinkError.Wrap(fullExitError(err))
}
return nil
}
// ListObjects lists objects
func (client *Uplink) ListObjects(bucket, prefix string) ([]string, error) {
cmd := client.cmd("ls", "s3://"+bucket+"/"+prefix)
data, err := cmd.Output()
if err != nil {
return nil, UplinkError.Wrap(fullExitError(err))
}
names := strings.Split(strings.TrimRight(string(data), "\n"), "\n")
return names, nil
}