This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
s3_file.go
264 lines (242 loc) · 7.43 KB
/
s3_file.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package s3
import (
"bytes"
"io"
"os"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/spf13/afero"
)
// File represents a file in S3.
// It is not threadsafe.
type File struct {
bucket string
name string
s3Fs afero.Fs
s3API s3iface.S3API
// state
offset int
closed bool
// readdir state
readdirContinuationToken *string
readdirNotTruncated bool
}
// NewFile initializes an File object.
func NewFile(bucket, name string, s3API s3iface.S3API, s3Fs afero.Fs) *File {
return &File{
bucket: bucket,
name: name,
s3API: s3API,
s3Fs: s3Fs,
offset: 0,
closed: false,
}
}
// Name returns the filename, i.e. S3 path without the bucket name.
func (f *File) Name() string { return f.name }
// Readdir reads the contents of the directory associated with file and
// returns a slice of up to n FileInfo values, as would be returned
// by ListObjects, in directory order. Subsequent calls on the same file will yield further FileInfos.
//
// If n > 0, Readdir returns at most n FileInfo structures. In this case, if
// Readdir returns an empty slice, it will return a non-nil error
// explaining why. At the end of a directory, the error is io.EOF.
//
// If n <= 0, Readdir returns all the FileInfo from the directory in
// a single slice. In this case, if Readdir succeeds (reads all
// the way to the end of the directory), it returns the slice and a
// nil error. If it encounters an error before the end of the
// directory, Readdir returns the FileInfo read until that point
// and a non-nil error.
func (f *File) Readdir(n int) ([]os.FileInfo, error) {
if f.readdirNotTruncated {
return nil, io.EOF
}
if n <= 0 {
return f.ReaddirAll()
}
// ListObjects treats leading slashes as part of the directory name
// It also needs a trailing slash to list contents of a directory.
name := trimLeadingSlash(f.Name()) + "/"
output, err := f.s3API.ListObjectsV2(&s3.ListObjectsV2Input{
ContinuationToken: f.readdirContinuationToken,
Bucket: aws.String(f.bucket),
Prefix: aws.String(name),
Delimiter: aws.String("/"),
MaxKeys: aws.Int64(int64(n)),
})
if err != nil {
return nil, err
}
f.readdirContinuationToken = output.NextContinuationToken
if !(*output.IsTruncated) {
f.readdirNotTruncated = true
}
fis := []os.FileInfo{}
for _, subfolder := range output.CommonPrefixes {
fis = append(fis, NewFileInfo(filepath.Base("/"+*subfolder.Prefix), true, 0, time.Time{}))
}
for _, fileObject := range output.Contents {
if hasTrailingSlash(*fileObject.Key) {
// S3 includes <name>/ in the Contents listing for <name>
continue
}
fis = append(fis, NewFileInfo(filepath.Base("/"+*fileObject.Key), false, *fileObject.Size, *fileObject.LastModified))
}
return fis, nil
}
// ReaddirAll provides list of file info.
func (f *File) ReaddirAll() ([]os.FileInfo, error) {
fileInfos := []os.FileInfo{}
for {
infos, err := f.Readdir(100)
fileInfos = append(fileInfos, infos...)
if err != nil {
if err == io.EOF {
break
} else {
return nil, err
}
}
}
return fileInfos, nil
}
// Readdirnames reads and returns a slice of names from the directory f.
//
// If n > 0, Readdirnames returns at most n names. In this case, if
// Readdirnames returns an empty slice, it will return a non-nil error
// explaining why. At the end of a directory, the error is io.EOF.
//
// If n <= 0, Readdirnames returns all the names from the directory in
// a single slice. In this case, if Readdirnames succeeds (reads all
// the way to the end of the directory), it returns the slice and a
// nil error. If it encounters an error before the end of the
// directory, Readdirnames returns the names read until that point and
// a non-nil error.
func (f *File) Readdirnames(n int) ([]string, error) {
fi, err := f.Readdir(n)
names := make([]string, len(fi))
for i, f := range fi {
_, names[i] = filepath.Split(f.Name())
}
return names, err
}
// Stat returns the FileInfo structure describing file.
// If there is an error, it will be of type *PathError.
func (f *File) Stat() (os.FileInfo, error) {
return f.s3Fs.Stat(f.Name())
}
// Sync is a noop.
func (f *File) Sync() error {
return nil
}
// Truncate changes the size of the file.
// It does not change the I/O offset.
// If there is an error, it will be of type *PathError.
func (f *File) Truncate(size int64) error {
panic("implement Truncate")
}
// WriteString is like Write, but writes the contents of string s rather than
// a slice of bytes.
func (f *File) WriteString(s string) (int, error) {
return f.Write([]byte(s))
}
// Close closes the File, rendering it unusable for I/O.
// It returns an error, if any.
func (f *File) Close() error {
f.closed = true
return nil
}
// Read reads up to len(b) bytes from the File.
// It returns the number of bytes read and an error, if any.
// EOF is signaled by a zero count with err set to io.EOF.
func (f *File) Read(p []byte) (int, error) {
if f.closed {
// mimic os.File's read after close behavior
panic("read after close")
}
if f.offset != 0 {
panic("TODO: non-offset == 0 read")
}
if len(p) == 0 {
return 0, nil
}
output, err := f.s3API.GetObject(&s3.GetObjectInput{
Bucket: aws.String(f.bucket),
Key: aws.String(f.name),
})
if err != nil {
return 0, err
}
defer output.Body.Close()
n, err := output.Body.Read(p)
f.offset += n
return n, err
}
// ReadAt reads len(p) bytes from the file starting at byte offset off.
// It returns the number of bytes read and the error, if any.
// ReadAt always returns a non-nil error when n < len(b).
// At end of file, that error is io.EOF.
func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
_, err = f.Seek(off, 0)
if err != nil {
return
}
n, err = f.Read(p)
return
}
// Seek sets the offset for the next Read or Write on file to offset, interpreted
// according to whence: 0 means relative to the origin of the file, 1 means
// relative to the current offset, and 2 means relative to the end.
// It returns the new offset and an error, if any.
// The behavior of Seek on a file opened with O_APPEND is not specified.
func (f *File) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0:
f.offset = int(offset)
case 1:
f.offset += int(offset)
case 2:
// can probably do this if we had GetObjectOutput (ContentLength)
panic("TODO: whence == 2 seek")
}
return int64(f.offset), nil
}
// Write writes len(b) bytes to the File.
// It returns the number of bytes written and an error, if any.
// Write returns a non-nil error when n != len(b).
func (f *File) Write(p []byte) (int, error) {
if f.closed {
// mimic os.File's write after close behavior
panic("write after close")
}
if f.offset != 0 {
panic("TODO: non-offset == 0 write")
}
readSeeker := bytes.NewReader(p)
size := int(readSeeker.Size())
if _, err := f.s3API.PutObject(&s3.PutObjectInput{
Bucket: aws.String(f.bucket),
Key: aws.String(f.name),
Body: readSeeker,
ServerSideEncryption: aws.String("AES256"),
}); err != nil {
return 0, err
}
f.offset += size
return size, nil
}
// WriteAt writes len(p) bytes to the file starting at byte offset off.
// It returns the number of bytes written and an error, if any.
// WriteAt returns a non-nil error when n != len(p).
func (f *File) WriteAt(p []byte, off int64) (n int, err error) {
_, err = f.Seek(off, 0)
if err != nil {
return
}
n, err = f.Write(p)
return
}