2015-05-10 15:20:58 +00:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2015-06-14 12:47:11 +00:00
|
|
|
"fmt"
|
2015-05-10 15:20:58 +00:00
|
|
|
"io"
|
2015-06-14 12:47:11 +00:00
|
|
|
"io/ioutil"
|
2015-05-10 15:20:58 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/goamz/aws"
|
2015-05-13 17:48:52 +00:00
|
|
|
"github.com/mitchellh/goamz/s3"
|
2015-05-10 15:20:58 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/backend"
|
|
|
|
)
|
|
|
|
|
2015-05-13 17:48:52 +00:00
|
|
|
const maxKeysInList = 1000
|
2015-06-14 12:17:38 +00:00
|
|
|
const connLimit = 10
|
2015-06-14 13:02:29 +00:00
|
|
|
const backendPrefix = "restic"
|
2015-05-10 15:20:58 +00:00
|
|
|
|
|
|
|
func s3path(t backend.Type, name string) string {
|
|
|
|
if t == backend.Config {
|
2015-06-14 13:02:29 +00:00
|
|
|
return backendPrefix + "/" + string(t)
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
2015-06-14 13:02:29 +00:00
|
|
|
return backendPrefix + "/" + string(t) + "/" + name
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-06-14 13:08:39 +00:00
|
|
|
type S3Backend struct {
|
2015-06-14 12:17:38 +00:00
|
|
|
bucket *s3.Bucket
|
|
|
|
connChan chan struct{}
|
|
|
|
path string
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 22:29:48 +00:00
|
|
|
// Open a backend using an S3 bucket object
|
2015-06-14 13:08:39 +00:00
|
|
|
func OpenS3Bucket(bucket *s3.Bucket, bucketname string) *S3Backend {
|
2015-06-14 12:17:38 +00:00
|
|
|
connChan := make(chan struct{}, connLimit)
|
|
|
|
for i := 0; i < connLimit; i++ {
|
|
|
|
connChan <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2015-06-14 13:08:39 +00:00
|
|
|
return &S3Backend{bucket: bucket, path: bucketname, connChan: connChan}
|
2015-05-15 22:29:48 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 17:48:52 +00:00
|
|
|
// Open opens the s3 backend at bucket and region.
|
2015-06-14 12:17:38 +00:00
|
|
|
func Open(regionname, bucketname string) (backend.Backend, error) {
|
2015-05-10 15:20:58 +00:00
|
|
|
auth, err := aws.EnvAuth()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-13 17:48:52 +00:00
|
|
|
client := s3.New(auth, aws.Regions[regionname])
|
2015-05-10 15:20:58 +00:00
|
|
|
|
2015-06-14 12:17:38 +00:00
|
|
|
return OpenS3Bucket(client.Bucket(bucketname), bucketname), nil
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 17:48:52 +00:00
|
|
|
// Location returns this backend's location (the bucket name).
|
2015-06-14 13:08:39 +00:00
|
|
|
func (be *S3Backend) Location() string {
|
|
|
|
return be.path
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type s3Blob struct {
|
2015-06-14 13:08:39 +00:00
|
|
|
b *S3Backend
|
2015-05-10 15:20:58 +00:00
|
|
|
buf *bytes.Buffer
|
|
|
|
final bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *s3Blob) Write(p []byte) (int, error) {
|
|
|
|
if bb.final {
|
|
|
|
return 0, errors.New("blob already closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := bb.buf.Write(p)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *s3Blob) Read(p []byte) (int, error) {
|
|
|
|
return bb.buf.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *s3Blob) Close() error {
|
|
|
|
bb.final = true
|
|
|
|
bb.buf.Reset()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *s3Blob) Size() uint {
|
|
|
|
return uint(bb.buf.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *s3Blob) Finalize(t backend.Type, name string) error {
|
|
|
|
if bb.final {
|
|
|
|
return errors.New("Already finalized")
|
|
|
|
}
|
|
|
|
|
|
|
|
bb.final = true
|
|
|
|
|
|
|
|
path := s3path(t, name)
|
2015-05-16 11:48:55 +00:00
|
|
|
|
|
|
|
// Check key does not already exist
|
|
|
|
key, err := bb.b.bucket.GetKey(path)
|
|
|
|
if err == nil && key.Key == path {
|
|
|
|
return errors.New("key already exists!")
|
|
|
|
}
|
|
|
|
|
2015-06-14 12:17:38 +00:00
|
|
|
<-bb.b.connChan
|
2015-05-16 11:48:55 +00:00
|
|
|
err = bb.b.bucket.Put(path, bb.buf.Bytes(), "binary/octet-stream", "private")
|
2015-06-14 12:17:38 +00:00
|
|
|
bb.b.connChan <- struct{}{}
|
2015-05-10 15:20:58 +00:00
|
|
|
bb.buf.Reset()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a new Blob. The data is available only after Finalize()
|
|
|
|
// has been called on the returned Blob.
|
2015-06-14 13:08:39 +00:00
|
|
|
func (be *S3Backend) Create() (backend.Blob, error) {
|
2015-05-10 15:20:58 +00:00
|
|
|
blob := s3Blob{
|
2015-06-14 13:08:39 +00:00
|
|
|
b: be,
|
2015-05-10 15:20:58 +00:00
|
|
|
buf: &bytes.Buffer{},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &blob, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a reader that yields the content stored under the given
|
|
|
|
// name. The reader should be closed after draining it.
|
2015-06-14 13:08:39 +00:00
|
|
|
func (be *S3Backend) Get(t backend.Type, name string) (io.ReadCloser, error) {
|
2015-06-14 12:47:11 +00:00
|
|
|
path := s3path(t, name)
|
2015-06-14 13:08:39 +00:00
|
|
|
return be.bucket.GetReader(path)
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetReader returns an io.ReadCloser for the Blob with the given name of
|
|
|
|
// type t at offset and length. If length is 0, the reader reads until EOF.
|
2015-06-14 13:08:39 +00:00
|
|
|
func (be *S3Backend) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) {
|
|
|
|
rc, err := be.Get(t, name)
|
2015-05-10 15:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-14 12:47:11 +00:00
|
|
|
n, errc := io.CopyN(ioutil.Discard, rc, int64(offset))
|
|
|
|
if errc != nil {
|
|
|
|
return nil, errc
|
|
|
|
} else if n != int64(offset) {
|
|
|
|
return nil, fmt.Errorf("less bytes read than expected, read: %d, expected: %d", n, offset)
|
|
|
|
}
|
2015-05-10 15:20:58 +00:00
|
|
|
|
|
|
|
if length == 0 {
|
2015-06-14 12:47:11 +00:00
|
|
|
return rc, nil
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-06-14 12:47:11 +00:00
|
|
|
return backend.LimitReadCloser(rc, int64(length)), nil
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test returns true if a blob of the given type and name exists in the backend.
|
2015-06-14 13:08:39 +00:00
|
|
|
func (be *S3Backend) Test(t backend.Type, name string) (bool, error) {
|
2015-05-10 15:20:58 +00:00
|
|
|
found := false
|
|
|
|
path := s3path(t, name)
|
2015-06-14 13:08:39 +00:00
|
|
|
key, err := be.bucket.GetKey(path)
|
2015-05-10 15:20:58 +00:00
|
|
|
if err == nil && key.Key == path {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If error, then not found
|
|
|
|
return found, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the blob with the given name and type.
|
2015-06-14 13:08:39 +00:00
|
|
|
func (be *S3Backend) Remove(t backend.Type, name string) error {
|
2015-05-10 15:20:58 +00:00
|
|
|
path := s3path(t, name)
|
2015-06-14 13:08:39 +00:00
|
|
|
return be.bucket.Del(path)
|
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.
|
2015-06-14 13:08:39 +00:00
|
|
|
func (be *S3Backend) List(t backend.Type, done <-chan struct{}) <-chan string {
|
2015-05-10 15:20:58 +00:00
|
|
|
ch := make(chan string)
|
|
|
|
|
2015-06-14 13:02:29 +00:00
|
|
|
prefix := s3path(t, "")
|
2015-05-10 15:20:58 +00:00
|
|
|
|
2015-06-14 13:08:39 +00:00
|
|
|
listresp, err := be.bucket.List(prefix, "/", "", maxKeysInList)
|
2015-05-10 15:20:58 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := make([]string, len(listresp.Contents))
|
|
|
|
for idx, key := range listresp.Contents {
|
|
|
|
matches[idx] = strings.TrimPrefix(key.Key, prefix)
|
|
|
|
}
|
|
|
|
|
2015-05-13 17:48:52 +00:00
|
|
|
// Continue making requests to get full list.
|
|
|
|
for listresp.IsTruncated {
|
2015-06-14 13:08:39 +00:00
|
|
|
listresp, err = be.bucket.List(prefix, "/", listresp.NextMarker, maxKeysInList)
|
2015-05-13 17:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range listresp.Contents {
|
|
|
|
matches = append(matches, strings.TrimPrefix(key.Key, prefix))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-10 15:20:58 +00:00
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
for _, m := range matches {
|
|
|
|
if m == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ch <- m:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2015-06-14 13:02:29 +00:00
|
|
|
// Remove keys for a specified backend type
|
2015-06-14 13:08:39 +00:00
|
|
|
func (be *S3Backend) removeKeys(t backend.Type) {
|
2015-06-14 13:02:29 +00:00
|
|
|
doneChan := make(chan struct{})
|
2015-06-14 13:08:39 +00:00
|
|
|
for key := range be.List(backend.Data, doneChan) {
|
|
|
|
be.Remove(backend.Data, key)
|
2015-06-14 13:02:29 +00:00
|
|
|
}
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes all restic keys
|
2015-06-14 13:08:39 +00:00
|
|
|
func (be *S3Backend) Delete() error {
|
|
|
|
be.removeKeys(backend.Data)
|
|
|
|
be.removeKeys(backend.Key)
|
|
|
|
be.removeKeys(backend.Lock)
|
|
|
|
be.removeKeys(backend.Snapshot)
|
|
|
|
be.removeKeys(backend.Index)
|
|
|
|
be.removeKeys(backend.Config)
|
2015-06-14 13:02:29 +00:00
|
|
|
return nil
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close does nothing
|
2015-06-14 13:08:39 +00:00
|
|
|
func (be *S3Backend) Close() error { return nil }
|