2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-19 19:45:18 +00:00
restic/internal/backend/s3/s3.go

512 lines
12 KiB
Go
Raw Normal View History

2015-05-10 15:20:58 +00:00
package s3
import (
2017-06-03 15:39:57 +00:00
"context"
s3: Use low-level API with a Range header for Load benchmark old ns/op new ns/op delta BenchmarkBackendMinio/LoadFile-4 9213315 11001787 +19.41% BenchmarkBackendMinio/LoadPartialFile-4 4176619 3479707 -16.69% BenchmarkBackendMinio/LoadPartialFileOffset-4 4391521 3139214 -28.52% BenchmarkBackendS3/LoadFile-4 2886070905 2505907501 -13.17% BenchmarkBackendS3/LoadPartialFile-4 762702722 735694398 -3.54% BenchmarkBackendS3/LoadPartialFileOffset-4 789724328 1108989142 +40.43% benchmark old MB/s new MB/s speedup BenchmarkBackendMinio/LoadFile-4 1821.21 1525.15 0.84x BenchmarkBackendMinio/LoadPartialFile-4 1004.49 1205.67 1.20x BenchmarkBackendMinio/LoadPartialFileOffset-4 955.34 1336.45 1.40x BenchmarkBackendS3/LoadFile-4 5.81 6.70 1.15x BenchmarkBackendS3/LoadPartialFile-4 5.50 5.70 1.04x BenchmarkBackendS3/LoadPartialFileOffset-4 5.31 3.78 0.71x benchmark old allocs new allocs delta BenchmarkBackendMinio/LoadFile-4 406 204 -49.75% BenchmarkBackendMinio/LoadPartialFile-4 225 206 -8.44% BenchmarkBackendMinio/LoadPartialFileOffset-4 227 207 -8.81% BenchmarkBackendS3/LoadFile-4 600 388 -35.33% BenchmarkBackendS3/LoadPartialFile-4 416 302 -27.40% BenchmarkBackendS3/LoadPartialFileOffset-4 417 303 -27.34% benchmark old bytes new bytes delta BenchmarkBackendMinio/LoadFile-4 29475 13904 -52.83% BenchmarkBackendMinio/LoadPartialFile-4 4218838 13958 -99.67% BenchmarkBackendMinio/LoadPartialFileOffset-4 4219175 14332 -99.66% BenchmarkBackendS3/LoadFile-4 114152 97424 -14.65% BenchmarkBackendS3/LoadPartialFile-4 4265416 56212 -98.68% BenchmarkBackendS3/LoadPartialFileOffset-4 4266520 56308 -98.68%
2017-05-13 19:18:14 +00:00
"fmt"
2015-05-10 15:20:58 +00:00
"io"
"net/http"
2017-05-13 21:55:22 +00:00
"os"
"path"
2015-05-10 15:20:58 +00:00
"strings"
"time"
2015-05-10 15:20:58 +00:00
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/errors"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
"github.com/minio/minio-go"
2017-07-05 14:19:25 +00:00
"github.com/minio/minio-go/pkg/credentials"
2015-05-10 15:20:58 +00:00
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/debug"
2015-05-10 15:20:58 +00:00
)
2017-06-07 19:59:01 +00:00
// Backend stores data on an S3 endpoint.
type Backend struct {
2017-06-15 14:41:09 +00:00
client *minio.Client
sem *backend.Semaphore
cfg Config
2017-04-11 20:04:18 +00:00
backend.Layout
}
2017-06-07 19:59:01 +00:00
// make sure that *Backend implements backend.Backend
var _ restic.Backend = &Backend{}
2017-06-03 15:39:57 +00:00
const defaultLayout = "default"
func open(cfg Config, rt http.RoundTripper) (*Backend, error) {
2016-09-27 20:35:08 +00:00
debug.Log("open, config %#v", cfg)
2015-05-10 15:20:58 +00:00
if cfg.MaxRetries > 0 {
minio.MaxRetry = int(cfg.MaxRetries)
}
// Chains all credential types, starting with
// Static credentials provided by user.
// IAM profile based credentials. (performs an HTTP
// call to a pre-defined endpoint, only valid inside
// configured ec2 instances)
// AWS env variables such as AWS_ACCESS_KEY_ID
// Minio env variables such as MINIO_ACCESS_KEY
creds := credentials.NewChainCredentials([]credentials.Provider{
&credentials.Static{
Value: credentials.Value{
AccessKeyID: cfg.KeyID,
SecretAccessKey: cfg.Secret,
},
},
&credentials.IAM{
Client: &http.Client{
Transport: http.DefaultTransport,
},
},
&credentials.EnvAWS{},
&credentials.EnvMinio{},
})
client, err := minio.NewWithCredentials(cfg.Endpoint, creds, !cfg.UseHTTP, "")
if err != nil {
return nil, errors.Wrap(err, "minio.NewWithCredentials")
}
2017-06-05 22:17:39 +00:00
sem, err := backend.NewSemaphore(cfg.Connections)
if err != nil {
return nil, err
}
2017-06-07 19:59:01 +00:00
be := &Backend{
2017-06-15 14:41:09 +00:00
client: client,
sem: sem,
cfg: cfg,
}
2017-02-10 18:24:54 +00:00
client.SetCustomTransport(rt)
2017-02-10 18:24:54 +00:00
l, err := backend.ParseLayout(be, cfg.Layout, defaultLayout, cfg.Prefix)
if err != nil {
return nil, err
}
be.Layout = l
2017-06-17 20:15:58 +00:00
return be, nil
}
// Open opens the S3 backend at bucket and region. The bucket is created if it
// does not exist yet.
func Open(cfg Config, rt http.RoundTripper) (restic.Backend, error) {
return open(cfg, rt)
2017-06-17 20:15:58 +00:00
}
// Create opens the S3 backend at bucket and region and creates the bucket if
// it does not exist yet.
func Create(cfg Config, rt http.RoundTripper) (restic.Backend, error) {
be, err := open(cfg, rt)
2017-07-17 08:33:19 +00:00
if err != nil {
return nil, errors.Wrap(err, "open")
}
2017-06-17 20:15:58 +00:00
found, err := be.client.BucketExists(cfg.Bucket)
2016-08-21 14:14:58 +00:00
if err != nil {
debug.Log("BucketExists(%v) returned err %v", cfg.Bucket, err)
2016-08-29 19:54:50 +00:00
return nil, errors.Wrap(err, "client.BucketExists")
2016-08-21 14:14:58 +00:00
}
if !found {
// create new bucket with default ACL in default region
2017-06-17 20:15:58 +00:00
err = be.client.MakeBucket(cfg.Bucket, "")
if err != nil {
2016-08-29 19:54:50 +00:00
return nil, errors.Wrap(err, "client.MakeBucket")
}
}
return be, nil
}
// IsNotExist returns true if the error is caused by a not existing file.
2017-06-07 19:59:01 +00:00
func (be *Backend) IsNotExist(err error) bool {
debug.Log("IsNotExist(%T, %#v)", err, err)
2017-06-16 08:54:46 +00:00
if os.IsNotExist(errors.Cause(err)) {
return true
}
if e, ok := errors.Cause(err).(minio.ErrorResponse); ok && e.Code == "NoSuchKey" {
return true
}
return false
}
// Join combines path components with slashes.
2017-06-07 19:59:01 +00:00
func (be *Backend) Join(p ...string) string {
return path.Join(p...)
}
type fileInfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
isDir bool
}
func (fi fileInfo) Name() string { return fi.name } // base name of the file
func (fi fileInfo) Size() int64 { return fi.size } // length in bytes for regular files; system-dependent for others
func (fi fileInfo) Mode() os.FileMode { return fi.mode } // file mode bits
func (fi fileInfo) ModTime() time.Time { return fi.modTime } // modification time
func (fi fileInfo) IsDir() bool { return fi.isDir } // abbreviation for Mode().IsDir()
func (fi fileInfo) Sys() interface{} { return nil } // underlying data source (can return nil)
// ReadDir returns the entries for a directory.
2017-06-07 19:59:01 +00:00
func (be *Backend) ReadDir(dir string) (list []os.FileInfo, err error) {
debug.Log("ReadDir(%v)", dir)
// make sure dir ends with a slash
if dir[len(dir)-1] != '/' {
dir += "/"
}
done := make(chan struct{})
defer close(done)
2017-06-15 14:41:09 +00:00
for obj := range be.client.ListObjects(be.cfg.Bucket, dir, false, done) {
if obj.Key == "" {
continue
}
name := strings.TrimPrefix(obj.Key, dir)
// Sometimes s3 returns an entry for the dir itself. Ignore it.
if name == "" {
continue
}
entry := fileInfo{
name: name,
size: obj.Size,
modTime: obj.LastModified,
}
if name[len(name)-1] == '/' {
entry.isDir = true
entry.mode = os.ModeDir | 0755
entry.name = name[:len(name)-1]
} else {
entry.mode = 0644
}
list = append(list, entry)
}
return list, nil
}
// Location returns this backend's location (the bucket name).
2017-06-07 19:59:01 +00:00
func (be *Backend) Location() string {
2017-06-15 14:41:09 +00:00
return be.Join(be.cfg.Bucket, be.cfg.Prefix)
}
// Path returns the path in the bucket that is used for this backend.
func (be *Backend) Path() string {
2017-06-15 14:41:09 +00:00
return be.cfg.Prefix
2015-05-10 15:20:58 +00:00
}
// nopCloserFile wraps *os.File and overwrites the Close() method with method
// that does nothing. In addition, the method Len() is implemented, which
// returns the size of the file (filesize - current offset).
type nopCloserFile struct {
*os.File
}
func (f nopCloserFile) Close() error {
debug.Log("prevented Close()")
return nil
}
// Len returns the remaining length of the file (filesize - current offset).
func (f nopCloserFile) Len() int {
debug.Log("Len() called")
fi, err := f.Stat()
if err != nil {
panic(err)
}
pos, err := f.Seek(0, io.SeekCurrent)
if err != nil {
panic(err)
}
size := fi.Size() - pos
debug.Log("returning file size %v", size)
return int(size)
}
type lenner interface {
Len() int
io.Reader
}
// nopCloserLenner wraps a lenner and overwrites the Close() method with method
// that does nothing. In addition, the method Size() is implemented, which
// returns the size of the file (filesize - current offset).
type nopCloserLenner struct {
lenner
}
func (f *nopCloserLenner) Close() error {
debug.Log("prevented Close()")
return nil
}
2016-01-24 00:15:35 +00:00
// Save stores data in the backend at the handle.
2017-06-07 19:59:01 +00:00
func (be *Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (err error) {
debug.Log("Save %v", h)
2016-01-24 00:15:35 +00:00
if err := h.Valid(); err != nil {
return err
}
2017-04-11 20:04:18 +00:00
objName := be.Filename(h)
2017-04-17 17:18:47 +00:00
// Check key does not already exist
2017-06-15 14:41:09 +00:00
_, err = be.client.StatObject(be.cfg.Bucket, objName)
if err == nil {
2016-09-27 20:35:08 +00:00
debug.Log("%v already exists", h)
return errors.New("key already exists")
}
// FIXME: This is a workaround once we move to minio-go 4.0.x this can be
// removed and size can be directly provided.
if f, ok := rd.(*os.File); ok {
debug.Log("reader is %#T, using nopCloserFile{}", rd)
rd = nopCloserFile{f}
} else if l, ok := rd.(lenner); ok {
debug.Log("reader is %#T, using nopCloserLenner{}", rd)
rd = nopCloserLenner{l}
} else {
debug.Log("reader is %#T, no specific workaround enabled", rd)
}
be.sem.GetToken()
2017-06-15 14:41:09 +00:00
debug.Log("PutObject(%v, %v)", be.cfg.Bucket, objName)
n, err := be.client.PutObject(be.cfg.Bucket, objName, rd, "application/octet-stream")
2017-06-05 22:17:39 +00:00
be.sem.ReleaseToken()
debug.Log("%v -> %v bytes, err %#v: %v", objName, n, err, err)
2016-01-24 00:15:35 +00:00
2016-08-29 19:54:50 +00:00
return errors.Wrap(err, "client.PutObject")
2016-01-24 00:15:35 +00: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 17:11:10 +00:00
// Load returns a reader that yields the contents of the file at h at the
2017-01-22 21:01:12 +00:00
// given offset. If length is nonzero, only a portion of the file is
// returned. rd must be closed after use.
2017-06-07 19:59:01 +00:00
func (be *Backend) Load(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
2017-04-17 17:18:47 +00:00
debug.Log("Load %v, length %v, offset %v from %v", h, length, offset, be.Filename(h))
2017-01-22 21:01:12 +00: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)
}
2017-04-11 20:04:18 +00:00
objName := be.Filename(h)
2017-01-22 21:01:12 +00:00
s3: Use low-level API with a Range header for Load benchmark old ns/op new ns/op delta BenchmarkBackendMinio/LoadFile-4 9213315 11001787 +19.41% BenchmarkBackendMinio/LoadPartialFile-4 4176619 3479707 -16.69% BenchmarkBackendMinio/LoadPartialFileOffset-4 4391521 3139214 -28.52% BenchmarkBackendS3/LoadFile-4 2886070905 2505907501 -13.17% BenchmarkBackendS3/LoadPartialFile-4 762702722 735694398 -3.54% BenchmarkBackendS3/LoadPartialFileOffset-4 789724328 1108989142 +40.43% benchmark old MB/s new MB/s speedup BenchmarkBackendMinio/LoadFile-4 1821.21 1525.15 0.84x BenchmarkBackendMinio/LoadPartialFile-4 1004.49 1205.67 1.20x BenchmarkBackendMinio/LoadPartialFileOffset-4 955.34 1336.45 1.40x BenchmarkBackendS3/LoadFile-4 5.81 6.70 1.15x BenchmarkBackendS3/LoadPartialFile-4 5.50 5.70 1.04x BenchmarkBackendS3/LoadPartialFileOffset-4 5.31 3.78 0.71x benchmark old allocs new allocs delta BenchmarkBackendMinio/LoadFile-4 406 204 -49.75% BenchmarkBackendMinio/LoadPartialFile-4 225 206 -8.44% BenchmarkBackendMinio/LoadPartialFileOffset-4 227 207 -8.81% BenchmarkBackendS3/LoadFile-4 600 388 -35.33% BenchmarkBackendS3/LoadPartialFile-4 416 302 -27.40% BenchmarkBackendS3/LoadPartialFileOffset-4 417 303 -27.34% benchmark old bytes new bytes delta BenchmarkBackendMinio/LoadFile-4 29475 13904 -52.83% BenchmarkBackendMinio/LoadPartialFile-4 4218838 13958 -99.67% BenchmarkBackendMinio/LoadPartialFileOffset-4 4219175 14332 -99.66% BenchmarkBackendS3/LoadFile-4 114152 97424 -14.65% BenchmarkBackendS3/LoadPartialFile-4 4265416 56212 -98.68% BenchmarkBackendS3/LoadPartialFileOffset-4 4266520 56308 -98.68%
2017-05-13 19:18:14 +00:00
byteRange := fmt.Sprintf("bytes=%d-", offset)
if length > 0 {
byteRange = fmt.Sprintf("bytes=%d-%d", offset, offset+int64(length)-1)
2017-01-22 21:01:12 +00:00
}
s3: Use low-level API with a Range header for Load benchmark old ns/op new ns/op delta BenchmarkBackendMinio/LoadFile-4 9213315 11001787 +19.41% BenchmarkBackendMinio/LoadPartialFile-4 4176619 3479707 -16.69% BenchmarkBackendMinio/LoadPartialFileOffset-4 4391521 3139214 -28.52% BenchmarkBackendS3/LoadFile-4 2886070905 2505907501 -13.17% BenchmarkBackendS3/LoadPartialFile-4 762702722 735694398 -3.54% BenchmarkBackendS3/LoadPartialFileOffset-4 789724328 1108989142 +40.43% benchmark old MB/s new MB/s speedup BenchmarkBackendMinio/LoadFile-4 1821.21 1525.15 0.84x BenchmarkBackendMinio/LoadPartialFile-4 1004.49 1205.67 1.20x BenchmarkBackendMinio/LoadPartialFileOffset-4 955.34 1336.45 1.40x BenchmarkBackendS3/LoadFile-4 5.81 6.70 1.15x BenchmarkBackendS3/LoadPartialFile-4 5.50 5.70 1.04x BenchmarkBackendS3/LoadPartialFileOffset-4 5.31 3.78 0.71x benchmark old allocs new allocs delta BenchmarkBackendMinio/LoadFile-4 406 204 -49.75% BenchmarkBackendMinio/LoadPartialFile-4 225 206 -8.44% BenchmarkBackendMinio/LoadPartialFileOffset-4 227 207 -8.81% BenchmarkBackendS3/LoadFile-4 600 388 -35.33% BenchmarkBackendS3/LoadPartialFile-4 416 302 -27.40% BenchmarkBackendS3/LoadPartialFileOffset-4 417 303 -27.34% benchmark old bytes new bytes delta BenchmarkBackendMinio/LoadFile-4 29475 13904 -52.83% BenchmarkBackendMinio/LoadPartialFile-4 4218838 13958 -99.67% BenchmarkBackendMinio/LoadPartialFileOffset-4 4219175 14332 -99.66% BenchmarkBackendS3/LoadFile-4 114152 97424 -14.65% BenchmarkBackendS3/LoadPartialFile-4 4265416 56212 -98.68% BenchmarkBackendS3/LoadPartialFileOffset-4 4266520 56308 -98.68%
2017-05-13 19:18:14 +00:00
headers := minio.NewGetReqHeaders()
headers.Add("Range", byteRange)
be.sem.GetToken()
s3: Use low-level API with a Range header for Load benchmark old ns/op new ns/op delta BenchmarkBackendMinio/LoadFile-4 9213315 11001787 +19.41% BenchmarkBackendMinio/LoadPartialFile-4 4176619 3479707 -16.69% BenchmarkBackendMinio/LoadPartialFileOffset-4 4391521 3139214 -28.52% BenchmarkBackendS3/LoadFile-4 2886070905 2505907501 -13.17% BenchmarkBackendS3/LoadPartialFile-4 762702722 735694398 -3.54% BenchmarkBackendS3/LoadPartialFileOffset-4 789724328 1108989142 +40.43% benchmark old MB/s new MB/s speedup BenchmarkBackendMinio/LoadFile-4 1821.21 1525.15 0.84x BenchmarkBackendMinio/LoadPartialFile-4 1004.49 1205.67 1.20x BenchmarkBackendMinio/LoadPartialFileOffset-4 955.34 1336.45 1.40x BenchmarkBackendS3/LoadFile-4 5.81 6.70 1.15x BenchmarkBackendS3/LoadPartialFile-4 5.50 5.70 1.04x BenchmarkBackendS3/LoadPartialFileOffset-4 5.31 3.78 0.71x benchmark old allocs new allocs delta BenchmarkBackendMinio/LoadFile-4 406 204 -49.75% BenchmarkBackendMinio/LoadPartialFile-4 225 206 -8.44% BenchmarkBackendMinio/LoadPartialFileOffset-4 227 207 -8.81% BenchmarkBackendS3/LoadFile-4 600 388 -35.33% BenchmarkBackendS3/LoadPartialFile-4 416 302 -27.40% BenchmarkBackendS3/LoadPartialFileOffset-4 417 303 -27.34% benchmark old bytes new bytes delta BenchmarkBackendMinio/LoadFile-4 29475 13904 -52.83% BenchmarkBackendMinio/LoadPartialFile-4 4218838 13958 -99.67% BenchmarkBackendMinio/LoadPartialFileOffset-4 4219175 14332 -99.66% BenchmarkBackendS3/LoadFile-4 114152 97424 -14.65% BenchmarkBackendS3/LoadPartialFile-4 4265416 56212 -98.68% BenchmarkBackendS3/LoadPartialFileOffset-4 4266520 56308 -98.68%
2017-05-13 19:18:14 +00:00
debug.Log("Load(%v) send range %v", h, byteRange)
2017-01-22 21:01:12 +00:00
2017-06-05 22:17:39 +00:00
coreClient := minio.Core{Client: be.client}
2017-06-15 14:41:09 +00:00
rd, _, err := coreClient.GetObject(be.cfg.Bucket, objName, headers)
2017-05-13 22:09:59 +00:00
if err != nil {
2017-06-05 22:17:39 +00:00
be.sem.ReleaseToken()
2017-05-13 22:09:59 +00:00
return nil, err
}
2017-01-22 21:01:12 +00:00
2017-05-13 22:09:59 +00:00
closeRd := wrapReader{
ReadCloser: rd,
f: func() {
debug.Log("Close()")
2017-06-05 22:17:39 +00:00
be.sem.ReleaseToken()
2017-05-13 22:09:59 +00:00
},
}
2017-01-22 21:01:12 +00:00
2017-05-13 22:09:59 +00:00
return closeRd, err
2017-01-22 21:01:12 +00:00
}
2016-01-23 22:27:58 +00:00
// Stat returns information about a blob.
2017-06-07 19:59:01 +00:00
func (be *Backend) Stat(ctx context.Context, h restic.Handle) (bi restic.FileInfo, err error) {
2016-09-27 20:35:08 +00:00
debug.Log("%v", h)
2017-04-11 20:04:18 +00:00
objName := be.Filename(h)
var obj *minio.Object
2017-06-15 14:41:09 +00:00
obj, err = be.client.GetObject(be.cfg.Bucket, objName)
2016-01-23 22:27:58 +00:00
if err != nil {
2016-09-27 20:35:08 +00:00
debug.Log("GetObject() err %v", err)
2016-08-31 20:39:36 +00:00
return restic.FileInfo{}, errors.Wrap(err, "client.GetObject")
2016-01-23 22:27:58 +00:00
}
// make sure that the object is closed properly.
defer func() {
e := obj.Close()
if err == nil {
2016-08-29 19:54:50 +00:00
err = errors.Wrap(e, "Close")
}
}()
2016-01-23 22:27:58 +00:00
fi, err := obj.Stat()
if err != nil {
2016-09-27 20:35:08 +00:00
debug.Log("Stat() err %v", err)
2016-08-31 20:39:36 +00:00
return restic.FileInfo{}, errors.Wrap(err, "Stat")
2016-01-23 22:27:58 +00:00
}
2016-08-31 20:39:36 +00:00
return restic.FileInfo{Size: fi.Size}, nil
2016-01-23 22:27:58 +00:00
}
2015-05-10 15:20:58 +00:00
// Test returns true if a blob of the given type and name exists in the backend.
2017-06-07 19:59:01 +00:00
func (be *Backend) Test(ctx context.Context, h restic.Handle) (bool, error) {
2015-05-10 15:20:58 +00:00
found := false
2017-04-11 20:04:18 +00:00
objName := be.Filename(h)
2017-06-15 14:41:09 +00:00
_, err := be.client.StatObject(be.cfg.Bucket, objName)
if err == nil {
2015-05-10 15:20:58 +00:00
found = true
}
// If error, then not found
return found, nil
}
// Remove removes the blob with the given name and type.
2017-06-07 19:59:01 +00:00
func (be *Backend) Remove(ctx context.Context, h restic.Handle) error {
2017-04-11 20:04:18 +00:00
objName := be.Filename(h)
2017-06-15 14:41:09 +00:00
err := be.client.RemoveObject(be.cfg.Bucket, objName)
2017-04-17 17:18:47 +00:00
debug.Log("Remove(%v) at %v -> err %v", h, objName, err)
if be.IsNotExist(err) {
err = nil
}
2016-08-29 19:54:50 +00:00
return errors.Wrap(err, "client.RemoveObject")
2015-05-10 15:20:58 +00: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.
2017-06-07 19:59:01 +00:00
func (be *Backend) List(ctx context.Context, t restic.FileType) <-chan string {
2016-09-27 20:35:08 +00:00
debug.Log("listing %v", t)
2015-05-10 15:20:58 +00:00
ch := make(chan string)
2017-04-11 20:04:18 +00:00
prefix := be.Dirname(restic.Handle{Type: t})
2015-05-10 15:20:58 +00:00
// make sure prefix ends with a slash
if prefix[len(prefix)-1] != '/' {
prefix += "/"
}
2017-06-15 14:41:09 +00:00
listresp := be.client.ListObjects(be.cfg.Bucket, prefix, true, ctx.Done())
2015-05-10 15:20:58 +00:00
go func() {
defer close(ch)
for obj := range listresp {
2015-12-28 23:27:29 +00:00
m := strings.TrimPrefix(obj.Key, prefix)
2015-05-10 15:20:58 +00:00
if m == "" {
continue
}
select {
2017-05-31 19:22:55 +00:00
case ch <- path.Base(m):
2017-06-03 15:39:57 +00:00
case <-ctx.Done():
2015-05-10 15:20:58 +00:00
return
}
}
}()
return ch
}
// Remove keys for a specified backend type.
2017-06-07 19:59:01 +00:00
func (be *Backend) removeKeys(ctx context.Context, t restic.FileType) error {
2017-06-03 15:39:57 +00:00
for key := range be.List(ctx, restic.DataFile) {
err := be.Remove(ctx, restic.Handle{Type: restic.DataFile, Name: key})
if err != nil {
return err
}
}
return nil
}
// Delete removes all restic keys in the bucket. It will not remove the bucket itself.
2017-06-07 19:59:01 +00:00
func (be *Backend) Delete(ctx context.Context) error {
2016-08-31 20:39:36 +00:00
alltypes := []restic.FileType{
restic.DataFile,
restic.KeyFile,
restic.LockFile,
restic.SnapshotFile,
restic.IndexFile}
for _, t := range alltypes {
2017-06-03 15:39:57 +00:00
err := be.removeKeys(ctx, t)
if err != nil {
return nil
}
}
2017-06-03 15:39:57 +00:00
return be.Remove(ctx, restic.Handle{Type: restic.ConfigFile})
2015-05-10 15:20:58 +00:00
}
// Close does nothing
2017-06-07 19:59:01 +00:00
func (be *Backend) Close() error { return nil }
// Rename moves a file based on the new layout l.
func (be *Backend) Rename(h restic.Handle, l backend.Layout) error {
debug.Log("Rename %v to %v", h, l)
oldname := be.Filename(h)
newname := l.Filename(h)
if oldname == newname {
debug.Log(" %v is already renamed", newname)
return nil
}
debug.Log(" %v -> %v", oldname, newname)
2017-07-17 18:43:45 +00:00
src := minio.NewSourceInfo(be.cfg.Bucket, oldname, nil)
dst, err := minio.NewDestinationInfo(be.cfg.Bucket, newname, nil, nil)
if err != nil {
return errors.Wrap(err, "NewDestinationInfo")
}
err = be.client.CopyObject(dst, src)
if err != nil && be.IsNotExist(err) {
debug.Log("copy failed: %v, seems to already have been renamed", err)
return nil
}
if err != nil {
debug.Log("copy failed: %v", err)
return err
}
2017-06-15 14:41:09 +00:00
return be.client.RemoveObject(be.cfg.Bucket, oldname)
}