Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MaxResults when listing objects #1824

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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