Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit 1a14efa

Browse files
committed
Uses IsTruncated and NextMarker for S3 list internal pagination
1 parent b522fbd commit 1a14efa

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

storagedriver/s3/s3.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,18 @@ func (d *S3Driver) ResumeWritePosition(path string) (uint64, error) {
192192
}
193193

194194
func (d *S3Driver) List(prefix string) ([]string, error) {
195-
listResponse, err := d.Bucket.List(prefix+"/", "/", "", listPartsMax)
195+
if prefix[len(prefix)-1] != '/' {
196+
prefix = prefix + "/"
197+
}
198+
listResponse, err := d.Bucket.List(prefix, "/", "", listPartsMax)
196199
if err != nil {
197200
return nil, err
198201
}
199202

200203
files := []string{}
201204
directories := []string{}
202205

203-
for len(listResponse.Contents) > 0 || len(listResponse.CommonPrefixes) > 0 {
206+
for {
204207
for _, key := range listResponse.Contents {
205208
files = append(files, key.Key)
206209
}
@@ -209,27 +212,13 @@ func (d *S3Driver) List(prefix string) ([]string, error) {
209212
directories = append(directories, commonPrefix[0:len(commonPrefix)-1])
210213
}
211214

212-
lastFile := ""
213-
lastDirectory := ""
214-
lastMarker := ""
215-
216-
if len(files) > 0 {
217-
lastFile = files[len(files)-1]
218-
}
219-
220-
if len(directories) > 0 {
221-
lastDirectory = directories[len(directories)-1] + "/"
222-
}
223-
224-
if lastDirectory > lastFile {
225-
lastMarker = lastDirectory
215+
if listResponse.IsTruncated {
216+
listResponse, err = d.Bucket.List(prefix, "/", listResponse.NextMarker, listPartsMax)
217+
if err != nil {
218+
return nil, err
219+
}
226220
} else {
227-
lastMarker = lastFile
228-
}
229-
230-
listResponse, err = d.Bucket.List(prefix+"/", "/", lastMarker, listPartsMax)
231-
if err != nil {
232-
return nil, err
221+
break
233222
}
234223
}
235224

0 commit comments

Comments
 (0)