Home Download Docs Code Community
     1	/*
     2	Copyright 2011 The Perkeep Authors
     3	
     4	Licensed under the Apache License, Version 2.0 (the "License");
     5	you may not use this file except in compliance with the License.
     6	You may obtain a copy of the License at
     7	
     8	     http://www.apache.org/licenses/LICENSE-2.0
     9	
    10	Unless required by applicable law or agreed to in writing, software
    11	distributed under the License is distributed on an "AS IS" BASIS,
    12	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13	See the License for the specific language governing permissions and
    14	limitations under the License.
    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	}
Website layout inspired by memcached.
Content by the authors.