2023-07-05 14:48:09 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ObjExporter struct {
|
|
|
|
selector *ObjSelector
|
|
|
|
}
|
|
|
|
|
|
|
|
type PreGenerateInfo struct {
|
|
|
|
Buckets []string `json:"buckets"`
|
|
|
|
Objects []ObjInfo `json:"objects"`
|
|
|
|
ObjSize string `json:"obj_size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjInfo struct {
|
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
Object string `json:"object"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewObjExporter(selector *ObjSelector) *ObjExporter {
|
|
|
|
return &ObjExporter{selector: selector}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ObjExporter) ExportJSONPreGen(fileName string) error {
|
|
|
|
f, err := os.Create(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// there can be a lot of object, so manually form json
|
|
|
|
if _, err = f.WriteString(`{"objects":[`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketMap := make(map[string]struct{})
|
|
|
|
|
|
|
|
count, err := o.selector.Count()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var comma string
|
|
|
|
for i := 0; i < count; i++ {
|
2023-07-20 12:01:57 +00:00
|
|
|
info := o.selector.NextObject()
|
2023-07-05 14:48:09 +00:00
|
|
|
if info == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = f.WriteString(fmt.Sprintf(`%s{"bucket":"%s","object":"%s"}`, comma, info.S3Bucket, info.S3Key)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
comma = ","
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketMap[info.S3Bucket] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = f.WriteString(`],"buckets":[`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
comma = ""
|
|
|
|
for bucket := range bucketMap {
|
|
|
|
if _, err = f.WriteString(fmt.Sprintf(`%s"%s"`, comma, bucket)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
comma = ","
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.WriteString(`]}`)
|
|
|
|
return err
|
|
|
|
}
|