/
filesystem.go
52 lines (45 loc) · 1.29 KB
/
filesystem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package s3httpfilesystem
import (
"net/http"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
as3 "github.com/aws/aws-sdk-go/service/s3"
)
// Filesystem implements the http.FileSystem interface for an S3 bucket.
type Filesystem struct {
s3 s3Interface
bucket string
}
// New builds and returns a new Filesystem accessing an S3.
// Credentials come from the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
// environment variables.
func New(endpoint, region, bucket string) *Filesystem {
creds := credentials.NewEnvCredentials()
config := aws.NewConfig().
WithCredentials(creds).
WithEndpoint(endpoint).
WithRegion(region)
sess := session.Must(session.NewSession(config))
return &Filesystem{
s3: as3.New(sess),
bucket: bucket,
}
}
// NewWithS3 takes an s3Interface and is used for testing
func NewWithS3(bucket string, mock s3Interface) *Filesystem {
return &Filesystem{
s3: mock,
bucket: bucket,
}
}
// Open returns an http.File interface for an object or simulated directory
// within the S3 bucket.
func (f *Filesystem) Open(path string) (http.File, error) {
return &File{fs: f, path: prepPath(path)}, nil
}
func prepPath(path string) string {
path = strings.TrimPrefix(path, "/")
return path
}