wip: s3 file prefix filter
This commit is contained in:
@ -30,7 +30,7 @@ func Init(ctx context.Context) error {
|
||||
register("/api/connection/connect", handler.ConnectionConnect)
|
||||
register("/api/connection/disconnect", handler.ConnectionDisconnect)
|
||||
register("/api/connection/buckets", handler.ConnectionBuckets)
|
||||
register("/api/bucket/file", handler.BucketFile)
|
||||
register("/api/bucket/files", handler.BucketFile)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -8,15 +8,16 @@ import (
|
||||
|
||||
func BucketFile(c *ndh.Ctx) error {
|
||||
type Req struct {
|
||||
ConnId uint64 `json:"conn_id"`
|
||||
Bucket string `json:"bucket"`
|
||||
Keyword string `json:"keyword"`
|
||||
ConnId uint64 `json:"conn_id"`
|
||||
Bucket string `json:"bucket"`
|
||||
Prefix string `json:"prefix"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = new(Req)
|
||||
client *s3.Client
|
||||
list []*s3.ListFileRes
|
||||
)
|
||||
|
||||
if err = c.ReqParse(req); err != nil {
|
||||
@ -31,5 +32,9 @@ func BucketFile(c *ndh.Ctx) error {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
client.ListFile()
|
||||
if list, err = client.ListFile(c.Context(), req.Bucket, req.Prefix); err != nil {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
return c.Send200(map[string]any{"list": list})
|
||||
}
|
||||
|
@ -15,10 +15,19 @@ type ListBucketRes struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type ListFileType int64
|
||||
|
||||
const (
|
||||
ListFileTypeFile ListFileType = iota
|
||||
ListFileTypeDir
|
||||
)
|
||||
|
||||
type ListFileRes struct {
|
||||
Name string
|
||||
LastModified time.Time
|
||||
Size int64
|
||||
Name string `json:"name"`
|
||||
Key string `json:"key"`
|
||||
LastModified time.Time `json:"last_modified"`
|
||||
Size int64 `json:"size"`
|
||||
Type ListFileType `json:"type"`
|
||||
}
|
||||
|
||||
func (c *Client) ListBucket(ctx context.Context) ([]*ListBucketRes, error) {
|
||||
@ -44,7 +53,7 @@ func (c *Client) ListBucket(ctx context.Context) ([]*ListBucketRes, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListFile(ctx context.Context, bucket string, prefix string, parent string) ([]*ListFileRes, error) {
|
||||
func (c *Client) ListFile(ctx context.Context, bucket string, prefix string) ([]*ListFileRes, error) {
|
||||
var (
|
||||
err error
|
||||
input = &s3.ListObjectsV2Input{
|
||||
@ -66,9 +75,11 @@ func (c *Client) ListFile(ctx context.Context, bucket string, prefix string, par
|
||||
folder := lo.FilterMap(
|
||||
output.CommonPrefixes,
|
||||
func(item types.CommonPrefix, index int) (*ListFileRes, bool) {
|
||||
name := strings.TrimPrefix(*item.Prefix, parent)
|
||||
name := strings.TrimPrefix(*item.Prefix, prefix)
|
||||
return &ListFileRes{
|
||||
Name: name,
|
||||
Key: name,
|
||||
Type: ListFileTypeDir,
|
||||
}, name != ""
|
||||
},
|
||||
)
|
||||
@ -77,9 +88,11 @@ func (c *Client) ListFile(ctx context.Context, bucket string, prefix string, par
|
||||
output.Contents,
|
||||
func(item types.Object, index int) *ListFileRes {
|
||||
return &ListFileRes{
|
||||
Name: *item.Key,
|
||||
Key: *item.Key,
|
||||
Name: strings.TrimPrefix(*item.Key, prefix),
|
||||
LastModified: *item.LastModified,
|
||||
Size: *item.Size,
|
||||
Type: ListFileTypeFile,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
@ -24,7 +24,7 @@ func TestListFile(t *testing.T) {
|
||||
t.Fatalf("call s3.New err = %s", err.Error())
|
||||
}
|
||||
|
||||
files, err := cli.ListFile(tool.Timeout(30), "infobox-person", "")
|
||||
files, err := cli.ListFile(tool.Timeout(30), "topic-audit", "")
|
||||
if err != nil {
|
||||
t.Fatalf("call s3.ListFile err = %s", err.Error())
|
||||
}
|
||||
@ -32,6 +32,6 @@ func TestListFile(t *testing.T) {
|
||||
t.Logf("[x] file length = %d", len(files))
|
||||
|
||||
for _, item := range files {
|
||||
t.Logf("[x] file = %s, size = %d", item.Name, item.Size)
|
||||
t.Logf("[x: %d] file = %s, size = %d", item.Type, item.Name, item.Size)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user