Skip to content

Commit

Permalink
Add support for MaxResults when listing objects
Browse files Browse the repository at this point in the history
  • Loading branch information
gaul committed Nov 26, 2024
1 parent 9e26890 commit 9afbb8a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -557,13 +561,22 @@ 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"),
Versions: r.URL.Query().Get("versions") == "true",
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}
Expand Down

0 comments on commit 9afbb8a

Please sign in to comment.