-
Notifications
You must be signed in to change notification settings - Fork 5
/
list.go
109 lines (91 loc) · 2.79 KB
/
list.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
package example
import (
"errors"
"log"
"go.beyondstorage.io/v5/pairs"
"go.beyondstorage.io/v5/types"
)
func ListAll(store types.Storager) {
// List all objects or files under the work dir.
//
// List needs at least one parameter.
// `path` is the directory path for file system, or a file hosting service like dropbox, also it could be a prefix filter for object storage.
//
// List will return two values.
// `oi` is an object iterator.
// `err` is the error during this operation.
it, err := store.List("")
if err != nil {
log.Fatalf("list: %v", err)
}
for {
// User can retrieve all the objects by `Next`. `types.IterateDone` will be returned while there is no item anymore.
o, err := it.Next()
if err != nil && !errors.Is(err, types.IterateDone) {
log.Fatalf("Next: %v", err)
}
if err != nil {
log.Printf("list completed")
break
}
log.Printf("object path: %v", o.Path)
}
}
func ListDir(store types.Storager, path string) {
// List with `types.ListModeDir` will list files or objects hierarchically.
// `path` is the directory path, or a file hosting service, also it could be a prefix filter(usually combined with delimiter `/` internally).
it, err := store.List(path, pairs.WithListMode(types.ListModeDir))
if err != nil {
log.Fatalf("list %v: %v", path, err)
}
for {
o, err := it.Next()
if err != nil && !errors.Is(err, types.IterateDone) {
log.Fatalf("Next %v: %v", path, err)
}
if err != nil {
log.Printf("list directory completed: %v", path)
break
}
log.Printf("object path: %v", o.Path)
}
}
func ListPrefix(store types.Storager, path string) {
// List with `types.ListModePrefix` will list files or objects with names contain the prefix.
// `path` is the prefix that the returned object names must contain.
it, err := store.List(path, pairs.WithListMode(types.ListModePrefix))
if err != nil {
log.Fatalf("list %v: %v", path, err)
}
for {
o, err := it.Next()
if err != nil && !errors.Is(err, types.IterateDone) {
log.Fatalf("Next %v: %v", path, err)
}
if err != nil {
log.Printf("list with prefix completed: %v", path)
break
}
log.Printf("object path: %v", o.Path)
}
}
func ListPart(store types.Storager, path string) {
// List with `types.ListModePart` could retrieve in-progress multipart uploads.
// `path` is the prefix that the returned object names must contain.
it, err := store.List(path, pairs.WithListMode(types.ListModePart))
if err != nil {
log.Fatalf("list %v: %v", path, err)
}
for {
o, err := it.Next()
if err != nil && !errors.Is(err, types.IterateDone) {
log.Fatalf("Next %v: %v", path, err)
}
if err != nil {
log.Printf("list multipart uploads completed: %v", path)
break
}
log.Printf("object path: %v", o.Path)
log.Printf("object multipartID: %v", o.MustGetMultipartID())
}
}