Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

cmd/serve/s3: return object listings in key order

Leon Brocard075552367ef2
diff --git a/cmd/serve/s3/pager.go b/cmd/serve/s3/pager.go
index 953f947d2..94c8e3cba 100644
--- a/cmd/serve/s3/pager.go
+++ b/cmd/serve/s3/pager.go
@@ -13,9 +13,9 @@ func (db *s3Backend) pager(list *gofakes3.ObjectList, page gofakes3.ListBucketPa
sort.Slice(list.CommonPrefixes, func(i, j int) bool {
return list.CommonPrefixes[i].Prefix < list.CommonPrefixes[j].Prefix
})
- // sort by modtime
+ // sort by key name
sort.Slice(list.Contents, func(i, j int) bool {
- return list.Contents[i].LastModified.Before(list.Contents[j].LastModified.Time)
+ return list.Contents[i].Key < list.Contents[j].Key
})
tokens := page.MaxKeys
if tokens == 0 {
diff --git a/cmd/serve/s3/pager_test.go b/cmd/serve/s3/pager_test.go
new file mode 100644
index 000000000..e7166b1aa
--- /dev/null
+++ b/cmd/serve/s3/pager_test.go
@@ -0,0 +1,32 @@
+package s3
+
+import (
+ "testing"
+ "time"
+
+ "github.com/rclone/gofakes3"
+)
+
+func TestPagerSortsContentsByKey(t *testing.T) {
+ list := gofakes3.NewObjectList()
+ list.Add(&gofakes3.Content{
+ Key: "b.txt",
+ LastModified: gofakes3.NewContentTime(time.Unix(100, 0)),
+ })
+ list.Add(&gofakes3.Content{
+ Key: "a.txt",
+ LastModified: gofakes3.NewContentTime(time.Unix(200, 0)),
+ })
+
+ got, err := (&s3Backend{}).pager(list, gofakes3.ListBucketPage{MaxKeys: 2})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(got.Contents) != 2 {
+ t.Fatalf("expected 2 contents, got %d", len(got.Contents))
+ }
+
+ if got.Contents[0].Key != "a.txt" || got.Contents[1].Key != "b.txt" {
+ t.Fatalf("expected lexicographic key order [a.txt b.txt], got [%s %s]", got.Contents[0].Key, got.Contents[1].Key)
+ }
+}