2015-05-10 10:20:58 -05:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2017-01-22 22:01:12 +01:00
|
|
|
"bytes"
|
2015-05-10 10:20:58 -05:00
|
|
|
"io"
|
2017-02-10 19:24:54 +01:00
|
|
|
"net/http"
|
2016-02-15 09:47:10 -08:00
|
|
|
"path"
|
2016-08-31 22:39:36 +02:00
|
|
|
"restic"
|
2015-05-10 10:20:58 -05:00
|
|
|
"strings"
|
2017-03-14 23:05:51 +01:00
|
|
|
"sync"
|
2015-05-10 10:20:58 -05:00
|
|
|
|
2017-01-22 22:01:12 +01:00
|
|
|
"restic/backend"
|
2016-09-01 22:17:37 +02:00
|
|
|
"restic/errors"
|
2016-08-21 17:46:23 +02:00
|
|
|
|
2015-11-06 15:31:59 -06:00
|
|
|
"github.com/minio/minio-go"
|
2015-05-10 10:20:58 -05:00
|
|
|
|
2016-02-14 15:29:28 +01:00
|
|
|
"restic/debug"
|
2015-05-10 10:20:58 -05:00
|
|
|
)
|
|
|
|
|
2017-04-17 19:18:58 +02:00
|
|
|
const connLimit = 10
|
2015-05-10 10:20:58 -05:00
|
|
|
|
2016-01-26 22:19:10 +01:00
|
|
|
// s3 is a backend which stores the data on an S3 endpoint.
|
|
|
|
type s3 struct {
|
2017-03-14 13:51:34 +01:00
|
|
|
client *minio.Client
|
|
|
|
connChan chan struct{}
|
|
|
|
bucketname string
|
|
|
|
prefix string
|
2017-03-14 23:05:51 +01:00
|
|
|
cacheMutex sync.RWMutex
|
|
|
|
cacheObjSize map[string]int64
|
2017-04-11 22:04:18 +02:00
|
|
|
backend.Layout
|
2015-05-15 17:29:48 -05:00
|
|
|
}
|
|
|
|
|
2015-12-28 18:55:15 +01:00
|
|
|
// Open opens the S3 backend at bucket and region. The bucket is created if it
|
|
|
|
// does not exist yet.
|
2016-08-31 22:39:36 +02:00
|
|
|
func Open(cfg Config) (restic.Backend, error) {
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("open, config %#v", cfg)
|
2015-05-10 10:20:58 -05:00
|
|
|
|
2016-06-08 21:33:18 +02:00
|
|
|
client, err := minio.New(cfg.Endpoint, cfg.KeyID, cfg.Secret, !cfg.UseHTTP)
|
2015-12-06 23:21:48 +01:00
|
|
|
if err != nil {
|
2016-08-29 21:54:50 +02:00
|
|
|
return nil, errors.Wrap(err, "minio.New")
|
2015-08-26 06:25:05 -05:00
|
|
|
}
|
|
|
|
|
2017-03-14 23:05:51 +01:00
|
|
|
be := &s3{
|
|
|
|
client: client,
|
|
|
|
bucketname: cfg.Bucket,
|
|
|
|
prefix: cfg.Prefix,
|
|
|
|
cacheObjSize: make(map[string]int64),
|
2017-04-17 19:18:47 +02:00
|
|
|
Layout: &backend.S3Layout{Path: cfg.Prefix, Join: path.Join},
|
2017-03-14 23:05:51 +01:00
|
|
|
}
|
2017-02-10 19:24:54 +01:00
|
|
|
|
2017-02-11 10:40:51 +01:00
|
|
|
tr := &http.Transport{MaxIdleConnsPerHost: connLimit}
|
|
|
|
client.SetCustomTransport(tr)
|
2017-02-10 19:24:54 +01:00
|
|
|
|
2015-12-06 23:21:48 +01:00
|
|
|
be.createConnections()
|
|
|
|
|
2016-12-05 23:12:30 +01:00
|
|
|
found, err := client.BucketExists(cfg.Bucket)
|
2016-08-21 16:14:58 +02:00
|
|
|
if err != nil {
|
2016-12-05 23:12:30 +01:00
|
|
|
debug.Log("BucketExists(%v) returned err %v", cfg.Bucket, err)
|
2016-08-29 21:54:50 +02:00
|
|
|
return nil, errors.Wrap(err, "client.BucketExists")
|
2016-08-21 16:14:58 +02:00
|
|
|
}
|
2015-12-28 18:55:15 +01:00
|
|
|
|
2016-12-05 23:12:30 +01:00
|
|
|
if !found {
|
2016-01-03 21:46:07 +01:00
|
|
|
// create new bucket with default ACL in default region
|
2016-04-18 21:29:17 +02:00
|
|
|
err = client.MakeBucket(cfg.Bucket, "")
|
2016-01-03 21:46:07 +01:00
|
|
|
if err != nil {
|
2016-08-29 21:54:50 +02:00
|
|
|
return nil, errors.Wrap(err, "client.MakeBucket")
|
2016-01-03 21:46:07 +01:00
|
|
|
}
|
2015-11-06 15:31:59 -06:00
|
|
|
}
|
|
|
|
|
2015-12-06 23:21:48 +01:00
|
|
|
return be, nil
|
|
|
|
}
|
|
|
|
|
2016-01-26 22:19:10 +01:00
|
|
|
func (be *s3) createConnections() {
|
2015-12-06 23:21:48 +01:00
|
|
|
be.connChan = make(chan struct{}, connLimit)
|
|
|
|
for i := 0; i < connLimit; i++ {
|
|
|
|
be.connChan <- struct{}{}
|
|
|
|
}
|
2015-05-10 10:20:58 -05:00
|
|
|
}
|
|
|
|
|
2015-05-13 12:48:52 -05:00
|
|
|
// Location returns this backend's location (the bucket name).
|
2016-01-26 22:19:10 +01:00
|
|
|
func (be *s3) Location() string {
|
2015-11-06 15:31:59 -06:00
|
|
|
return be.bucketname
|
2015-05-10 10:20:58 -05:00
|
|
|
}
|
|
|
|
|
2016-01-24 01:15:35 +01:00
|
|
|
// Save stores data in the backend at the handle.
|
2017-01-22 22:01:12 +01:00
|
|
|
func (be *s3) Save(h restic.Handle, rd io.Reader) (err error) {
|
2016-01-24 01:15:35 +01:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-11 22:04:18 +02:00
|
|
|
objName := be.Filename(h)
|
2016-01-24 01:15:35 +01:00
|
|
|
|
2017-04-17 19:18:47 +02:00
|
|
|
debug.Log("Save %v at %v", h, objName)
|
|
|
|
|
2016-01-24 21:13:24 +01:00
|
|
|
// Check key does not already exist
|
2016-02-15 09:47:10 -08:00
|
|
|
_, err = be.client.StatObject(be.bucketname, objName)
|
2016-01-24 21:13:24 +01:00
|
|
|
if err == nil {
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("%v already exists", h)
|
2016-01-24 21:13:24 +01:00
|
|
|
return errors.New("key already exists")
|
|
|
|
}
|
|
|
|
|
2016-01-24 01:15:35 +01:00
|
|
|
<-be.connChan
|
|
|
|
defer func() {
|
|
|
|
be.connChan <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
2017-01-22 12:32:20 +01:00
|
|
|
debug.Log("PutObject(%v, %v)",
|
|
|
|
be.bucketname, objName)
|
|
|
|
n, err := be.client.PutObject(be.bucketname, objName, rd, "binary/octet-stream")
|
2016-02-15 09:47:10 -08:00
|
|
|
debug.Log("%v -> %v bytes, err %#v", objName, n, err)
|
2016-01-24 01:15:35 +01:00
|
|
|
|
2016-08-29 21:54:50 +02:00
|
|
|
return errors.Wrap(err, "client.PutObject")
|
2016-01-24 01:15:35 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 19:25:50 +01:00
|
|
|
// wrapReader wraps an io.ReadCloser to run an additional function on Close.
|
|
|
|
type wrapReader struct {
|
|
|
|
io.ReadCloser
|
|
|
|
f func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wr wrapReader) Close() error {
|
|
|
|
err := wr.ReadCloser.Close()
|
|
|
|
wr.f()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:11:10 +01:00
|
|
|
// Load returns a reader that yields the contents of the file at h at the
|
2017-01-22 22:01:12 +01:00
|
|
|
// given offset. If length is nonzero, only a portion of the file is
|
|
|
|
// returned. rd must be closed after use.
|
2017-01-23 18:11:10 +01:00
|
|
|
func (be *s3) Load(h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
2017-04-17 19:18:47 +02:00
|
|
|
debug.Log("Load %v, length %v, offset %v from %v", h, length, offset, be.Filename(h))
|
2017-01-22 22:01:12 +01:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset < 0 {
|
|
|
|
return nil, errors.New("offset is negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
if length < 0 {
|
|
|
|
return nil, errors.Errorf("invalid length %d", length)
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj *minio.Object
|
2017-03-14 13:51:34 +01:00
|
|
|
var size int64
|
2017-01-22 22:01:12 +01:00
|
|
|
|
2017-04-11 22:04:18 +02:00
|
|
|
objName := be.Filename(h)
|
2017-01-22 22:01:12 +01:00
|
|
|
|
2017-02-10 20:49:37 +01:00
|
|
|
// get token for connection
|
2017-01-22 22:01:12 +01:00
|
|
|
<-be.connChan
|
|
|
|
|
|
|
|
obj, err := be.client.GetObject(be.bucketname, objName)
|
|
|
|
if err != nil {
|
|
|
|
debug.Log(" err %v", err)
|
2017-02-10 20:49:37 +01:00
|
|
|
|
|
|
|
// return token
|
|
|
|
be.connChan <- struct{}{}
|
|
|
|
|
2017-01-22 22:01:12 +01:00
|
|
|
return nil, errors.Wrap(err, "client.GetObject")
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we're going to read the whole object, just pass it on.
|
|
|
|
if length == 0 {
|
2017-01-23 18:11:10 +01:00
|
|
|
debug.Log("Load %v: pass on object", h)
|
2017-02-10 19:25:50 +01:00
|
|
|
|
2017-01-22 22:01:12 +01:00
|
|
|
_, err = obj.Seek(offset, 0)
|
|
|
|
if err != nil {
|
2017-01-23 16:20:07 +01:00
|
|
|
_ = obj.Close()
|
2017-02-10 20:49:37 +01:00
|
|
|
|
|
|
|
// return token
|
|
|
|
be.connChan <- struct{}{}
|
|
|
|
|
2017-01-22 22:01:12 +01:00
|
|
|
return nil, errors.Wrap(err, "obj.Seek")
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:25:50 +01:00
|
|
|
rd := wrapReader{
|
|
|
|
ReadCloser: obj,
|
|
|
|
f: func() {
|
2017-02-10 20:49:37 +01:00
|
|
|
debug.Log("Close()")
|
|
|
|
// return token
|
2017-02-10 19:25:50 +01:00
|
|
|
be.connChan <- struct{}{}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return rd, nil
|
2017-01-22 22:01:12 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 19:25:50 +01:00
|
|
|
defer func() {
|
2017-02-10 20:49:37 +01:00
|
|
|
// return token
|
2017-02-10 19:25:50 +01:00
|
|
|
be.connChan <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
2017-01-22 22:01:12 +01:00
|
|
|
// otherwise use a buffer with ReadAt
|
2017-03-14 23:05:51 +01:00
|
|
|
be.cacheMutex.RLock()
|
|
|
|
size, cacheHit := be.cacheObjSize[objName]
|
|
|
|
be.cacheMutex.RUnlock()
|
|
|
|
|
|
|
|
if !cacheHit {
|
2017-03-14 13:51:34 +01:00
|
|
|
info, err := obj.Stat()
|
|
|
|
if err != nil {
|
|
|
|
_ = obj.Close()
|
|
|
|
return nil, errors.Wrap(err, "obj.Stat")
|
|
|
|
}
|
|
|
|
size = info.Size
|
2017-03-14 23:05:51 +01:00
|
|
|
be.cacheMutex.Lock()
|
|
|
|
be.cacheObjSize[objName] = size
|
|
|
|
be.cacheMutex.Unlock()
|
2017-01-22 22:01:12 +01:00
|
|
|
}
|
|
|
|
|
2017-03-14 13:51:34 +01:00
|
|
|
if offset > size {
|
2017-01-23 16:20:07 +01:00
|
|
|
_ = obj.Close()
|
2017-02-10 19:39:49 +01:00
|
|
|
return nil, errors.New("offset larger than file size")
|
2017-01-22 22:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
l := int64(length)
|
2017-03-14 13:51:34 +01:00
|
|
|
if offset+l > size {
|
|
|
|
l = size - offset
|
2017-01-22 22:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, l)
|
|
|
|
n, err := obj.ReadAt(buf, offset)
|
2017-01-23 18:11:10 +01:00
|
|
|
debug.Log("Load %v: use buffer with ReadAt: %v, %v", h, n, err)
|
2017-01-22 22:01:12 +01:00
|
|
|
if err == io.EOF {
|
2017-01-23 18:11:10 +01:00
|
|
|
debug.Log("Load %v: shorten buffer %v -> %v", h, len(buf), n)
|
2017-01-22 22:01:12 +01:00
|
|
|
buf = buf[:n]
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-01-23 16:20:07 +01:00
|
|
|
_ = obj.Close()
|
2017-01-22 22:01:12 +01:00
|
|
|
return nil, errors.Wrap(err, "obj.ReadAt")
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend.Closer{Reader: bytes.NewReader(buf)}, nil
|
|
|
|
}
|
|
|
|
|
2016-01-23 23:27:58 +01:00
|
|
|
// Stat returns information about a blob.
|
2017-01-22 22:01:12 +01:00
|
|
|
func (be *s3) Stat(h restic.Handle) (bi restic.FileInfo, err error) {
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("%v", h)
|
2016-08-21 16:15:24 +02:00
|
|
|
|
2017-04-11 22:04:18 +02:00
|
|
|
objName := be.Filename(h)
|
2016-08-21 16:15:24 +02:00
|
|
|
var obj *minio.Object
|
|
|
|
|
2016-02-15 09:47:10 -08:00
|
|
|
obj, err = be.client.GetObject(be.bucketname, objName)
|
2016-01-23 23:27:58 +01:00
|
|
|
if err != nil {
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("GetObject() err %v", err)
|
2016-08-31 22:39:36 +02:00
|
|
|
return restic.FileInfo{}, errors.Wrap(err, "client.GetObject")
|
2016-01-23 23:27:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-21 16:15:24 +02:00
|
|
|
// make sure that the object is closed properly.
|
|
|
|
defer func() {
|
|
|
|
e := obj.Close()
|
|
|
|
if err == nil {
|
2016-08-29 21:54:50 +02:00
|
|
|
err = errors.Wrap(e, "Close")
|
2016-08-21 16:15:24 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-01-23 23:27:58 +01:00
|
|
|
fi, err := obj.Stat()
|
|
|
|
if err != nil {
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("Stat() err %v", err)
|
2016-08-31 22:39:36 +02:00
|
|
|
return restic.FileInfo{}, errors.Wrap(err, "Stat")
|
2016-01-23 23:27:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-31 22:39:36 +02:00
|
|
|
return restic.FileInfo{Size: fi.Size}, nil
|
2016-01-23 23:27:58 +01:00
|
|
|
}
|
|
|
|
|
2015-05-10 10:20:58 -05:00
|
|
|
// Test returns true if a blob of the given type and name exists in the backend.
|
2017-01-25 17:48:35 +01:00
|
|
|
func (be *s3) Test(h restic.Handle) (bool, error) {
|
2015-05-10 10:20:58 -05:00
|
|
|
found := false
|
2017-04-11 22:04:18 +02:00
|
|
|
objName := be.Filename(h)
|
2016-02-15 09:47:10 -08:00
|
|
|
_, err := be.client.StatObject(be.bucketname, objName)
|
2015-08-26 06:25:05 -05:00
|
|
|
if err == nil {
|
2015-05-10 10:20:58 -05:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If error, then not found
|
|
|
|
return found, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the blob with the given name and type.
|
2017-01-25 17:48:35 +01:00
|
|
|
func (be *s3) Remove(h restic.Handle) error {
|
2017-04-11 22:04:18 +02:00
|
|
|
objName := be.Filename(h)
|
2016-02-15 09:47:10 -08:00
|
|
|
err := be.client.RemoveObject(be.bucketname, objName)
|
2017-04-17 19:18:47 +02:00
|
|
|
debug.Log("Remove(%v) at %v -> err %v", h, objName, err)
|
2016-08-29 21:54:50 +02:00
|
|
|
return errors.Wrap(err, "client.RemoveObject")
|
2015-05-10 10:20:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// List returns a channel that yields all names of blobs of type t. A
|
|
|
|
// goroutine is started for this. If the channel done is closed, sending
|
|
|
|
// stops.
|
2016-08-31 22:39:36 +02:00
|
|
|
func (be *s3) List(t restic.FileType, done <-chan struct{}) <-chan string {
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("listing %v", t)
|
2015-05-10 10:20:58 -05:00
|
|
|
ch := make(chan string)
|
|
|
|
|
2017-04-11 22:04:18 +02:00
|
|
|
prefix := be.Dirname(restic.Handle{Type: t})
|
2015-05-10 10:20:58 -05:00
|
|
|
|
2015-12-29 00:27:29 +01:00
|
|
|
listresp := be.client.ListObjects(be.bucketname, prefix, true, done)
|
2015-05-13 12:48:52 -05:00
|
|
|
|
2015-05-10 10:20:58 -05:00
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
2015-11-06 15:31:59 -06:00
|
|
|
for obj := range listresp {
|
2015-12-29 00:27:29 +01:00
|
|
|
m := strings.TrimPrefix(obj.Key, prefix)
|
2015-05-10 10:20:58 -05:00
|
|
|
if m == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ch <- m:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2015-12-06 23:21:48 +01:00
|
|
|
// Remove keys for a specified backend type.
|
2016-08-31 22:39:36 +02:00
|
|
|
func (be *s3) removeKeys(t restic.FileType) error {
|
2015-12-06 23:21:48 +01:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
2016-08-31 22:39:36 +02:00
|
|
|
for key := range be.List(restic.DataFile, done) {
|
2017-01-25 17:48:35 +01:00
|
|
|
err := be.Remove(restic.Handle{Type: restic.DataFile, Name: key})
|
2015-12-06 23:21:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-14 08:02:29 -05:00
|
|
|
}
|
2015-12-06 23:21:48 +01:00
|
|
|
|
|
|
|
return nil
|
2015-06-14 08:02:29 -05:00
|
|
|
}
|
|
|
|
|
2015-12-19 13:23:05 +01:00
|
|
|
// Delete removes all restic keys in the bucket. It will not remove the bucket itself.
|
2016-01-26 22:19:10 +01:00
|
|
|
func (be *s3) Delete() error {
|
2016-08-31 22:39:36 +02:00
|
|
|
alltypes := []restic.FileType{
|
|
|
|
restic.DataFile,
|
|
|
|
restic.KeyFile,
|
|
|
|
restic.LockFile,
|
|
|
|
restic.SnapshotFile,
|
|
|
|
restic.IndexFile}
|
2015-12-06 23:21:48 +01:00
|
|
|
|
|
|
|
for _, t := range alltypes {
|
|
|
|
err := be.removeKeys(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
return be.Remove(restic.Handle{Type: restic.ConfigFile})
|
2015-05-10 10:20:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close does nothing
|
2016-01-26 22:19:10 +01:00
|
|
|
func (be *s3) Close() error { return nil }
|