github.com/puellanivis/breton@v0.2.16/lib/files/s3files/reader.go (about)

     1  package s3files
     2  
     3  import (
     4  	"context"
     5  	"net/url"
     6  	"time"
     7  
     8  	"github.com/puellanivis/breton/lib/files"
     9  	"github.com/puellanivis/breton/lib/files/wrapper"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/service/s3"
    13  )
    14  
    15  func (h *handler) Open(ctx context.Context, uri *url.URL) (files.Reader, error) {
    16  	bucket, key, err := getBucketKey("open", uri)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	cl, err := h.getClient(ctx, bucket)
    22  	if err != nil {
    23  		return nil, files.PathError("open", uri.String(), err)
    24  	}
    25  
    26  	req := &s3.GetObjectInput{
    27  		Bucket: aws.String(bucket),
    28  		Key:    aws.String(key),
    29  	}
    30  
    31  	res, err := cl.GetObjectWithContext(ctx, req)
    32  	if err != nil {
    33  		return nil, files.PathError("read", uri.String(), normalizeError(err))
    34  	}
    35  
    36  	var l int64
    37  	if res.ContentLength != nil {
    38  		l = *res.ContentLength
    39  	}
    40  
    41  	lm := time.Now()
    42  	if res.LastModified != nil {
    43  		lm = *res.LastModified
    44  	}
    45  
    46  	return wrapper.NewReaderWithInfo(res.Body, wrapper.NewInfo(uri, int(l), lm)), nil
    47  }