GeezerVison is showing github.com — setMaxResults doesn't seem to work for Cloud Storage API · Issue #1511 · googleapis/google-cloud-java
setMaxResults doesn't seem to work for Cloud Storage API #1511DescriptionI'm using:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>gcloud-java</artifactId>
<version>0.2.8</version>
</dependency>
which transitively depends on com.google.apis:google-api-services-storage:v1-rev62-1.21.0 and I have the following code:
Storage storage = ...
storage.objects().list(bucket).setMaxResults((long) Integer.MAX_VALUE).execute().getItems();
However the number of items are still 1000 (which is the default value) regardless of the value I pass to setMaxResults:
System.out.println(storage.objects().list(bucket).setMaxResults((long) Integer.MAX_VALUE).execute().size());
This yields 1000 as result. Activitygarrettjonesgoogle commented on Jan 11, 2017 Could you try using a more recent version? The artifact is now named google-cloud-java and the latest version is 0.8.1. johanhaleby commented on Jan 11, 2017 Oh I was unaware of the change of artifactId, thanks for the hint. I'll try to look into it in a few days if that's ok. johanhaleby commented on Jan 13, 2017 Is google-cloud-java in maven central? I can't seem to find it. garrettjonesgoogle commented on Jan 13, 2017 Oops sorry, the artifact is named google-cloud. garrettjonesgoogle commented on Jan 26, 2017 vam-google commented on Jan 31, 2017 Looks like it is by design. Though I agree, the API signature, naming and documentation are confusing.
Apparently Storage.setMaxResults() sets the maximum size for the results page, and the maximum allowed value for it is 1000 (not properly documented).
As a workaround the following code can be used (it will successfully read > 1000 objects, page by page):
public class MyStorageExample {
public static void main(String[] args) throws GeneralSecurityException, IOException {
Storage.Builder sb = new Builder(
GoogleNetHttpTransport.newTrustedTransport(),
new JacksonFactory(),
null);
Storage storage = sb.build();
Objects objects;
String pageToken = null;
final long maxPageSize = 1000L; // 1000 is the maximum possible value
final String bucketName = "<YOUR_BUCKET_NAME>";
do {
objects = storage
.objects()
.list(bucketName)
.setMaxResults(maxPageSize)
.setPageToken(pageToken)
.execute();
pageToken = objects.getNextPageToken();
if(objects.getItems() != null) {
System.out.println(objects.getItems().size());
}
} while (pageToken != null);
}
}johanhaleby commented on Feb 2, 2017 @vam-google I'm using something similar to what you're describing. I think it would be nice to improve the docs a bit to make it clear that 1000 is the maximum value.
Thanks for the support everyone. vam-google commented on Feb 9, 2017 The documentation and default value was updated for the Storage API, and will be available in the never version, once it is released. Closing the issue.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|