use of software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable in project aws-doc-sdk-examples by awsdocs.
the class S3ObjectOperations method main.
public static void main(String[] args) throws IOException {
final String USAGE = "\n" + "Usage:\n" + " <bucketName> <key>\n\n" + "Where:\n" + " bucketName - the Amazon S3 bucket to create.\n\n" + " key - the key to use.\n\n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String key = args[1];
// snippet-start:[s3.java2.s3_object_operations.upload]
Region region = Region.US_WEST_2;
s3 = S3Client.builder().region(region).build();
createBucket(s3, bucketName, region);
PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(key).build();
s3.putObject(objectRequest, RequestBody.fromByteBuffer(getRandomByteBuffer(10_000)));
// snippet-end:[s3.java2.s3_object_operations.upload]
// Multipart upload example
String multipartKey = "multiPartKey";
multipartUpload(bucketName, multipartKey);
// snippet-start:[s3.java2.s3_object_operations.pagination]
ListObjectsV2Request listObjectsReqManual = ListObjectsV2Request.builder().bucket(bucketName).maxKeys(1).build();
boolean done = false;
while (!done) {
ListObjectsV2Response listObjResponse = s3.listObjectsV2(listObjectsReqManual);
for (S3Object content : listObjResponse.contents()) {
System.out.println(content.key());
}
if (listObjResponse.nextContinuationToken() == null) {
done = true;
}
listObjectsReqManual = listObjectsReqManual.toBuilder().continuationToken(listObjResponse.nextContinuationToken()).build();
}
// snippet-end:[s3.java2.s3_object_operations.pagination]
// snippet-start:[s3.java2.s3_object_operations.iterative]
ListObjectsV2Request listReq = ListObjectsV2Request.builder().bucket(bucketName).maxKeys(1).build();
ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq);
// Process response pages
listRes.stream().flatMap(r -> r.contents().stream()).forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
// snippet-end:[s3.java2.s3_object_operations.iterative]
// snippet-start:[s3.java2.s3_object_operations.stream]
// Helper method to work with paginated collection of items directly
listRes.contents().stream().forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
// snippet-start:[s3.java2.s3_object_operations.forloop]
for (S3Object content : listRes.contents()) {
System.out.println(" Key: " + content.key() + " size = " + content.size());
}
// snippet-end:[s3.java2.s3_object_operations.forloop]
// snippet-start:[s3.java2.s3_object_operations.download]
GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucketName).key(key).build();
s3.getObject(getObjectRequest);
// snippet-end:[s3.java2.s3_object_operations.download]
// snippet-start:[s3.java2.s3_object_operations.delete]
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucketName).key(key).build();
s3.deleteObject(deleteObjectRequest);
// snippet-end:[s3.java2.s3_object_operations.delete]
// Delete an object
deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucketName).key(multipartKey).build();
s3.deleteObject(deleteObjectRequest);
deleteBucket(s3, bucketName);
System.out.println("Done");
}
use of software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable in project aws-doc-sdk-examples by awsdocs.
the class S3Scenario method anotherListExample.
public static void anotherListExample(S3Client s3, String bucketName) {
ListObjectsV2Request listReq = ListObjectsV2Request.builder().bucket(bucketName).maxKeys(1).build();
ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq);
// Process response pages
listRes.stream().flatMap(r -> r.contents().stream()).forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
// Helper method to work with paginated collection of items directly
listRes.contents().stream().forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
for (S3Object content : listRes.contents()) {
System.out.println(" Key: " + content.key() + " size = " + content.size());
}
}
Aggregations