From 9afbb8ab113d4a54c2431d56a70f77ddd56b63d4 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 24 Nov 2024 20:03:59 -0800 Subject: [PATCH] Add support for MaxResults when listing objects --- fakestorage/object.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fakestorage/object.go b/fakestorage/object.go index b229a45233..23a133aa30 100644 --- a/fakestorage/object.go +++ b/fakestorage/object.go @@ -315,6 +315,7 @@ type ListOptions struct { StartOffset string EndOffset string IncludeTrailingDelimiter bool + MaxResults int } // ListObjects returns a sorted list of objects that match the given criteria, @@ -365,6 +366,9 @@ func (s *Server) ListObjectsWithOptions(bucketName string, options ListOptions) respPrefixes = append(respPrefixes, p) } sort.Strings(respPrefixes) + if options.MaxResults != 0 && len(respObjects) > options.MaxResults { + respObjects = respObjects[:options.MaxResults] + } return respObjects, respPrefixes, nil } @@ -557,6 +561,14 @@ func (s *Server) objectWithGenerationOnValidGeneration(bucketName, objectName, g func (s *Server) listObjects(r *http.Request) jsonResponse { bucketName := unescapeMuxVars(mux.Vars(r))["bucketName"] + var maxResults int + var err error + if maxResultsStr := r.URL.Query().Get("maxResults"); maxResultsStr != "" { + maxResults, err = strconv.Atoi(maxResultsStr) + if err != nil { + return jsonResponse{status: http.StatusBadRequest} + } + } objs, prefixes, err := s.ListObjectsWithOptions(bucketName, ListOptions{ Prefix: r.URL.Query().Get("prefix"), Delimiter: r.URL.Query().Get("delimiter"), @@ -564,6 +576,7 @@ func (s *Server) listObjects(r *http.Request) jsonResponse { StartOffset: r.URL.Query().Get("startOffset"), EndOffset: r.URL.Query().Get("endOffset"), IncludeTrailingDelimiter: r.URL.Query().Get("includeTrailingDelimiter") == "true", + MaxResults: maxResults, }) if err != nil { return jsonResponse{status: http.StatusNotFound}