- Ignore directory (size <= 0) when listing bucket object
- Add test for empty directory
main
Martin Guibert 2021-04-07 11:01:16 +02:00 committed by sundowndev
parent 5f60cf44bc
commit 1ee31b4679
2 changed files with 58 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package enumerator
import (
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
@ -45,7 +46,9 @@ func (s *S3Enumerator) Enumerate() ([]string, error) {
}
err := s.client.ListObjectsV2Pages(input, func(output *s3.ListObjectsV2Output, lastPage bool) bool {
for _, metadata := range output.Contents {
keys = append(keys, strings.Join([]string{bucket, *metadata.Key}, "/"))
if aws.Int64Value(metadata.Size) > 0 {
keys = append(keys, strings.Join([]string{bucket, *metadata.Key}, "/"))
}
}
return !lastPage
})

View File

@ -37,26 +37,32 @@ func TestS3Enumerator_Enumerate(t *testing.T) {
callback(&s3.ListObjectsV2Output{
Contents: []*s3.Object{
{
Key: awssdk.String("a/nested/prefix/state1"),
Key: awssdk.String("a/nested/prefix/state1"),
Size: awssdk.Int64(5),
},
{
Key: awssdk.String("a/nested/prefix/state2"),
Key: awssdk.String("a/nested/prefix/state2"),
Size: awssdk.Int64(2),
},
{
Key: awssdk.String("a/nested/prefix/state3"),
Key: awssdk.String("a/nested/prefix/state3"),
Size: awssdk.Int64(1),
},
},
}, false)
callback(&s3.ListObjectsV2Output{
Contents: []*s3.Object{
{
Key: awssdk.String("a/nested/prefix/state4"),
Key: awssdk.String("a/nested/prefix/state4"),
Size: awssdk.Int64(5),
},
{
Key: awssdk.String("a/nested/prefix/state5"),
Key: awssdk.String("a/nested/prefix/state5"),
Size: awssdk.Int64(5),
},
{
Key: awssdk.String("a/nested/prefix/state6"),
Key: awssdk.String("a/nested/prefix/state6"),
Size: awssdk.Int64(5),
},
},
}, true)
@ -73,6 +79,48 @@ func TestS3Enumerator_Enumerate(t *testing.T) {
"bucket-name/a/nested/prefix/state6",
},
},
{
name: "test that directories objects are filtered",
config: config.SupplierConfig{
Path: "bucket-name/a/nested/prefix",
},
mocks: func(client *mocks.FakeS3) {
input := &s3.ListObjectsV2Input{
Bucket: awssdk.String("bucket-name"),
Prefix: awssdk.String("a/nested/prefix"),
}
client.On(
"ListObjectsV2Pages",
input,
mock.MatchedBy(func(callback func(res *s3.ListObjectsV2Output, lastPage bool) bool) bool {
callback(&s3.ListObjectsV2Output{
Contents: []*s3.Object{
{
Key: awssdk.String("a/nested/prefix/state1"),
Size: awssdk.Int64(0),
},
{
Key: awssdk.String("a/nested/prefix/state2"),
Size: nil,
},
{
Key: awssdk.String("a/nested/prefix/state3"),
Size: awssdk.Int64(-1),
},
{
Key: awssdk.String("a/nested/prefix/state4"),
Size: awssdk.Int64(1),
},
},
}, true)
return true
}),
).Return(nil)
},
want: []string{
"bucket-name/a/nested/prefix/state4",
},
},
{
name: "test when invalid config used",
config: config.SupplierConfig{