如何在Openstack4j中列出超过10000个Swift对象?
Fetching All Objects in a Swift Container (Beyond 10,000 Limit)
Absolutely! That 10,000-object limit is just Swift's default pagination cap—you can get around it using marker-based pagination, which is the standard way to list large numbers of objects in Swift. Here's a generic, reusable method to fetch every object in a container, no matter how many there are:
Step-by-Step Implementation
Swift's list() method supports optional marker and limit parameters that let you paginate through results. The marker tells Swift where to start the next page (right after the last object from the previous request), and limit lets you specify how many objects to fetch per page (max 10,000).
Generic Java Code Example
import java.util.ArrayList; import java.util.List; import org.jclouds.openstack.swift.v1.domain.SwiftObject; public List<SwiftObject> listAllObjects(String containerName, ObjectStorage objectStorage) { List<SwiftObject> allObjects = new ArrayList<>(); String lastObjectName = null; final int MAX_PAGE_SIZE = 10000; // Swift's maximum allowed per request do { // Fetch a single page of objects List<? extends SwiftObject> currentPage = objectStorage.objects().list( containerName, lastObjectName, MAX_PAGE_SIZE ); if (currentPage.isEmpty()) { break; // No more objects to retrieve } allObjects.addAll(currentPage); // Update marker to the last object's name for the next iteration lastObjectName = currentPage.get(currentPage.size() - 1).getName(); } while (true); return allObjects; }
Key Details
- Marker Usage: The
lastObjectNamevariable acts as our marker. For the first request, it'snull, so Swift starts from the beginning. Each subsequent request starts right after the last object from the previous page. - Max Page Size: Setting
MAX_PAGE_SIZEto 10000 ensures we're using the most efficient page size possible (since Swift won't return more than 10k objects per request anyway). - Termination Condition: The loop breaks when we get an empty page, which means we've fetched every object in the container.
Important Notes
- Add error handling in production code: Network blips, rate limits, or container changes can interrupt pagination—consider adding retry logic or exception catching inside the loop.
- Some higher-level Swift client libraries might have built-in methods for this (like
listAll()), but if you're working with the core API, this manual pagination approach is the universal solution.
内容的提问来源于stack exchange,提问作者Roman Nikitchenko
相关产品推荐
相关产品推荐

