-
Notifications
You must be signed in to change notification settings - Fork 5
/
httpfs.go
96 lines (71 loc) · 1.61 KB
/
httpfs.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
package example
import (
"io"
"log"
"go.beyondstorage.io/v5/pkg/fswrap"
"go.beyondstorage.io/v5/types"
)
func HttpFSOpen(store types.Storager, path string) {
fsys := fswrap.HttpFs(store)
f, err := fsys.Open(path)
if err != nil {
log.Fatalf("Open %v: %v", path, err)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
log.Fatalf("Stat %v: %v", path, err)
}
log.Printf("file name: %v", info.Name())
}
func HttpFSReadDir(store types.Storager, path string) {
fsys := fswrap.HttpFs(store)
f, err := fsys.Open(path)
if err != nil {
log.Fatalf("Open %v: %v", path, err)
}
defer f.Close()
list, err := f.Readdir(-1)
if err != nil {
log.Fatalf("Readdir %v: %v", path, err)
}
for _, info := range list {
log.Printf("file name: %v", info.Name())
}
}
func HttpFsRead(store types.Storager, path string) {
fsys := fswrap.HttpFs(store)
f, err := fsys.Open(path)
if err != nil {
log.Fatalf("Open %v: %v", path, err)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
log.Fatalf("Stat %v: %v", path, err)
}
size := info.Size()
data := make([]byte, 0, size+1)
n, err := f.Read(data)
if err != nil {
log.Fatalf("Read %v: %v", path, err)
}
log.Printf("read data length: %d", n)
}
func HttpFsSeek(store types.Storager, path string) {
fsys := fswrap.HttpFs(store)
f, err := fsys.Open(path)
if err != nil {
log.Fatalf("Open %v: %v", path, err)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
log.Fatalf("Stat %v: %v", path, err)
}
size := info.Size()
got, err := f.Seek(0, io.SeekEnd)
if err != nil || got != size {
log.Fatalf("Seek %v: %v", path, err)
}
}