1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
17 package s3
18
19 import (
20 "context"
21 "fmt"
22 "io"
23 "os"
24
25 "github.com/aws/aws-sdk-go/aws"
26 "github.com/aws/aws-sdk-go/aws/awserr"
27 "github.com/aws/aws-sdk-go/service/s3"
28 "perkeep.org/pkg/blob"
29 )
30
31 func (sto *s3Storage) Fetch(ctx context.Context, blob blob.Ref) (file io.ReadCloser, size uint32, err error) {
32 if faultGet.FailErr(&err) {
33 return
34 }
35 return sto.fetch(ctx, blob, nil)
36 }
37
38 func (sto *s3Storage) SubFetch(ctx context.Context, br blob.Ref, offset, length int64) (rc io.ReadCloser, err error) {
39 if offset < 0 || length < 0 {
40 return nil, blob.ErrNegativeSubFetch
41 }
42 rc, _, err = sto.fetch(ctx, br, aws.String(fmt.Sprintf("bytes=%d-%d", offset, offset+length-1)))
43 return
44 }
45
46 func (sto *s3Storage) fetch(ctx context.Context, br blob.Ref, objRange *string) (rc io.ReadCloser, size uint32, err error) {
47 resp, err := sto.client.GetObjectWithContext(ctx, &s3.GetObjectInput{
48 Bucket: &sto.bucket,
49 Key: aws.String(sto.dirPrefix + br.String()),
50 Range: objRange,
51 })
52 if err == nil {
53 return resp.Body, uint32(*resp.ContentLength), err
54 }
55 if resp.Body != nil {
56 resp.Body.Close()
57 }
58 if isNotFound(err) {
59 return nil, 0, os.ErrNotExist
60 }
61 if aerr, ok := err.(awserr.Error); ok {
62 if aerr.Code() == "InvalidRange" {
63 return nil, 0, blob.ErrOutOfRangeOffsetSubFetch
64 }
65 }
66 return nil, 0, err
67 }