aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/nsheridan/wkfs/s3/s3.go
blob: de44f93a082c70042b36807e5bf65abdd67a183a (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package s3

import (
	"bytes"
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"strings"
	"time"

	"go4.org/wkfs"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
)

// Options for registering the S3 wkfs.
// None of these are required and can be supplied to the aws client by other means.
type Options struct {
	Region    string
	AccessKey string
	SecretKey string
}

var _ wkfs.FileSystem = (*s3FS)(nil)

// Register the /s3/ filesystem as a well-known filesystem.
func Register(opts *Options) {
	if opts == nil {
		opts = &Options{}
	}
	config := &aws.Config{}
	// If region is unset the SDK will attempt to read the region from the environment.
	if opts.Region != "" {
		config.Region = aws.String(opts.Region)
	}
	// Attempt to use supplied credentials, otherwise fall back to the SDK.
	if opts.AccessKey != "" && opts.SecretKey != "" {
		config.Credentials = credentials.NewStaticCredentials(opts.AccessKey, opts.SecretKey, "")
	}
	s, err := session.NewSession(config)
	if err != nil {
		registerBrokenFS(err)
		return
	}
	sc := s3.New(s)
	if aws.StringValue(sc.Config.Region) == "" {
		registerBrokenFS(errors.New("could not find region configuration"))
		return
	}
	wkfs.RegisterFS("/s3/", &s3FS{
		sc: sc,
	})
}

func registerBrokenFS(err error) {
	wkfs.RegisterFS("/s3/", &s3FS{
		err: err,
	})
}

type s3FS struct {
	sc  *s3.S3
	err error
}

func (fs *s3FS) parseName(name string) (bucket, fileName string, err error) {
	if fs.err != nil {
		return "", "", fs.err
	}
	name = strings.TrimPrefix(name, "/s3/")
	i := strings.Index(name, "/")
	if i < 0 {
		return name, "", nil
	}
	return name[:i], name[i+1:], nil
}

// Open opens the named file for reading.
func (fs *s3FS) Open(name string) (wkfs.File, error) {
	bucket, fileName, err := fs.parseName(name)
	if err != nil {
		return nil, err
	}
	obj, err := fs.sc.GetObject(&s3.GetObjectInput{
		Bucket: &bucket,
		Key:    &fileName,
	})
	if err != nil {
		if aerr, ok := err.(awserr.Error); ok {
			switch aerr.Code() {
			case "NoSuchKey", "NoSuchBucket":
				return nil, os.ErrNotExist
			}
		}
		return nil, err
	}
	defer obj.Body.Close()
	slurp, err := ioutil.ReadAll(obj.Body)
	if err != nil {
		return nil, err
	}
	return &file{
		name:   name,
		Reader: bytes.NewReader(slurp),
	}, nil
}

func (fs *s3FS) Stat(name string) (os.FileInfo, error) { return fs.Lstat(name) }
func (fs *s3FS) Lstat(name string) (os.FileInfo, error) {
	bucket, fileName, err := fs.parseName(name)
	if err != nil {
		return nil, err
	}
	obj, err := fs.sc.GetObject(&s3.GetObjectInput{
		Bucket: &bucket,
		Key:    &fileName,
	})
	if err != nil {
		if awsErr, ok := err.(awserr.Error); ok {
			if awsErr.Code() == "NoSuchKey" {
				return nil, os.ErrNotExist
			}
		}
	}
	if err != nil {
		return nil, err
	}
	return &statInfo{
		name: path.Base(fileName),
		size: *obj.ContentLength,
	}, nil
}

func (fs *s3FS) MkdirAll(path string, perm os.FileMode) error {
	_, err := fs.OpenFile(fmt.Sprintf("%s/", filepath.Clean(path)), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
	return err
}

func (fs *s3FS) OpenFile(name string, flag int, perm os.FileMode) (wkfs.FileWriter, error) {
	bucket, filename, err := fs.parseName(name)
	if err != nil {
		return nil, err
	}
	switch flag {
	case os.O_WRONLY | os.O_CREATE | os.O_EXCL:
	case os.O_WRONLY | os.O_CREATE | os.O_TRUNC:
	default:
		return nil, fmt.Errorf("Unsupported OpenFlag flag mode %d on S3", flag)
	}
	if flag&os.O_EXCL != 0 {
		if _, err := fs.Stat(name); err == nil {
			return nil, os.ErrExist
		}
	}
	return NewS3file(bucket, filename, fs.sc)
}

func (fs *s3FS) Remove(name string) error {
	var err error
	bucket, filename, err := fs.parseName(name)
	if err != nil {
		return err
	}
	_, err = fs.sc.DeleteObject(&s3.DeleteObjectInput{
		Bucket: aws.String(bucket),
		Key:    aws.String(filename),
	})
	return err
}

type statInfo struct {
	name    string
	size    int64
	isDir   bool
	modtime time.Time
}

func (si *statInfo) IsDir() bool        { return si.isDir }
func (si *statInfo) ModTime() time.Time { return si.modtime }
func (si *statInfo) Mode() os.FileMode  { return 0644 }
func (si *statInfo) Name() string       { return path.Base(si.name) }
func (si *statInfo) Size() int64        { return si.size }
func (si *statInfo) Sys() interface{}   { return nil }

type file struct {
	name string
	*bytes.Reader
}

func (*file) Close() error   { return nil }
func (f *file) Name() string { return path.Base(f.name) }
func (f *file) Stat() (os.FileInfo, error) {
	panic("Stat not implemented on /s3/ files yet")
}