forked from lxfontes/s3dir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
90 lines (71 loc) · 1.49 KB
/
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
package s3dir
import (
"io"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/service/s3"
)
type File struct {
s3Object *s3.GetObjectOutput
bucket *Bucket
path string
}
func NewFile(bucket *Bucket, path string, object *s3.GetObjectOutput) *File {
return &File{
bucket: bucket,
path: path,
s3Object: object,
}
}
func (f *File) Stat() (os.FileInfo, error) {
return f, nil
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
// since s3 doesn't support seeking, we can only seek from the start of the file
if whence != io.SeekStart {
return 0, ErrNotImplemented
}
// reopen the file
nf, err := f.bucket.Open(f.path)
if err != nil {
return 0, err
}
f.s3Object = nf.(*File).s3Object
if _, err := f.Read(make([]byte, offset)); err != nil {
return 0, err
}
return 0, nil
}
func (f *File) Read(p []byte) (int, error) {
i, err := f.s3Object.Body.Read(p)
return i, err
}
func (f *File) Close() error {
return f.s3Object.Body.Close()
}
func (f *File) Name() string {
return strings.TrimRight(f.path, "/")
}
func (f *File) Size() int64 {
return *f.s3Object.ContentLength
}
func (f *File) Mode() os.FileMode {
return 0444
}
func (f *File) ModTime() time.Time {
if f.s3Object.LastModified == nil {
return time.Time{}
}
return *f.s3Object.LastModified
}
func (f *File) IsDir() bool {
return false
}
func (f *File) Sys() interface{} {
return f.s3Object
}
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
// Not a directory
return nil, nil
}