-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
86 lines (69 loc) · 1.74 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/fsouza/fake-gcs-server/fakestorage"
)
func generateObjectsFromFiles() []fakestorage.Object {
objects := []fakestorage.Object{}
// if a /data volume is mounted in the container
if _, err := os.Stat("/data"); !os.IsNotExist(err) {
// list the content
files, err := ioutil.ReadDir("/data")
if err != nil {
panic(err)
}
for _, f := range files {
fn := f.Name()
fi, err := os.Stat("/data/" + fn)
if err != nil {
panic(err)
}
switch mode := fi.Mode(); {
case mode.IsDir():
// if it's a directory, look for the files it's containing
files, err := ioutil.ReadDir("/data/" + fn)
if err != nil {
panic(err)
}
// for each file create an object
for _, f := range files {
fmt.Printf("Creating object with name %s in bucket %s\n", f.Name(), fn)
content, err := ioutil.ReadFile("/data/" + fn + "/" + f.Name())
if err != nil {
panic(err)
}
object := fakestorage.Object{
BucketName: fn,
Name: f.Name(), //filename
Content: content,
}
objects = append(objects, object)
}
}
}
}
return objects
}
func main() {
// initialObjects := []fakestorage.Object{
// {
// objectName: "object",
// Name: "object-precreate-object",
// Content: []byte("This object just forces the object to exist when the server starts up."),
// },
// }
loadedObjects := generateObjectsFromFiles()
server, err := fakestorage.NewServerWithOptions(fakestorage.Options{
InitialObjects: loadedObjects,
Host: "0.0.0.0",
Port: 4443,
StorageRoot: "/storage",
})
if err != nil {
panic(err)
}
fmt.Printf("Server started at %s\n", server.URL())
select {}
}