wip: s3 file prefix filter
This commit is contained in:
@ -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