1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-25 01:06:39 +00:00

Adapt s3 backend to new library

This commit is contained in:
Alexander Neumann 2016-01-07 21:06:54 +01:00
parent 0e9236475b
commit 289aee9448

View file

@ -3,7 +3,6 @@ package s3
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"io" "io"
"strings" "strings"
@ -120,7 +119,7 @@ func (bb *s3Blob) Finalize(t backend.Type, name string) error {
<-bb.b.connChan <-bb.b.connChan
debug.Log("s3.Finalize", "PutObject(%v, %v, %v, %v)", debug.Log("s3.Finalize", "PutObject(%v, %v, %v, %v)",
bb.b.bucketname, path, int64(bb.buf.Len()), "binary/octet-stream") bb.b.bucketname, path, int64(bb.buf.Len()), "binary/octet-stream")
n, err := bb.b.client.PutObject(bb.b.bucketname, path, bb.buf, int64(bb.buf.Len()), "binary/octet-stream") n, err := bb.b.client.PutObject(bb.b.bucketname, path, bb.buf, "binary/octet-stream")
debug.Log("s3.Finalize", "finalized %v -> n %v, err %#v", path, n, err) debug.Log("s3.Finalize", "finalized %v -> n %v, err %#v", path, n, err)
bb.b.connChan <- struct{}{} bb.b.connChan <- struct{}{}
@ -150,9 +149,13 @@ func (be *S3Backend) Create() (backend.Blob, error) {
// name. The reader should be closed after draining it. // name. The reader should be closed after draining it.
func (be *S3Backend) Get(t backend.Type, name string) (io.ReadCloser, error) { func (be *S3Backend) Get(t backend.Type, name string) (io.ReadCloser, error) {
path := s3path(t, name) path := s3path(t, name)
rc, _, err := be.client.GetObject(be.bucketname, path) rc, err := be.client.GetObject(be.bucketname, path)
debug.Log("s3.Get", "%v %v -> err %v", t, name, err) debug.Log("s3.Get", "%v %v -> err %v", t, name, err)
return rc, err if err != nil {
return nil, err
}
return rc, nil
} }
// GetReader returns an io.ReadCloser for the Blob with the given name of // GetReader returns an io.ReadCloser for the Blob with the given name of
@ -160,35 +163,24 @@ func (be *S3Backend) Get(t backend.Type, name string) (io.ReadCloser, error) {
func (be *S3Backend) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) { func (be *S3Backend) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) {
debug.Log("s3.GetReader", "%v %v, offset %v len %v", t, name, offset, length) debug.Log("s3.GetReader", "%v %v, offset %v len %v", t, name, offset, length)
path := s3path(t, name) path := s3path(t, name)
rd, stat, err := be.client.GetObjectPartial(be.bucketname, path) obj, err := be.client.GetObject(be.bucketname, path)
debug.Log("s3.GetReader", " stat %v, err %v", stat, err)
if err != nil { if err != nil {
debug.Log("s3.GetReader", " err %v", err)
return nil, err return nil, err
} }
l, o := int64(length), int64(offset) if offset > 0 {
_, err = obj.Seek(int64(offset), 0)
if l == 0 { if err != nil {
l = stat.Size return nil, err
}
} }
if o > stat.Size { if length == 0 {
return nil, fmt.Errorf("offset beyond end of file (%v > %v)", o, stat.Size) return obj, nil
} }
if o+l > stat.Size { return backend.LimitReadCloser(obj, int64(length)), nil
l = stat.Size - o
}
debug.Log("s3.GetReader", "%v %v, o %v l %v", t, name, o, l)
var r io.Reader
r = &ContinuousReader{R: rd, Offset: o}
if length > 0 {
r = io.LimitReader(r, int64(length))
}
return backend.ReadCloser(r), nil
} }
// Test returns true if a blob of the given type and name exists in the backend. // Test returns true if a blob of the given type and name exists in the backend.