From 4daaa5bb6c6496191c553f433ee79012abc29212 Mon Sep 17 00:00:00 2001 From: David Souther Date: Tue, 5 Dec 2023 16:08:34 -0500 Subject: [PATCH] Rust: S3 ListObject v2 using pagination. --- rustv1/examples/s3/src/s3-service-lib.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/rustv1/examples/s3/src/s3-service-lib.rs b/rustv1/examples/s3/src/s3-service-lib.rs index c7f5d266856..1a85fe9adba 100644 --- a/rustv1/examples/s3/src/s3-service-lib.rs +++ b/rustv1/examples/s3/src/s3-service-lib.rs @@ -71,11 +71,25 @@ pub async fn delete_objects(client: &Client, bucket_name: &str) -> Result Result<(), Error> { - let objects = client.list_objects_v2().bucket(bucket_name).send().await?; - println!("Objects in bucket:"); - for obj in objects.contents() { - println!("{:?}", obj.key().unwrap()); +pub async fn list_objects(client: &Client, bucket: &str) -> Result<(), Error> { + let mut response = client + .list_objects_v2() + .bucket(bucket.to_owned()) + .max_keys(10) // In this example, go 10 at a time. + .into_paginator() + .send(); + + while let Some(result) = response.next().await { + match result { + Ok(output) => { + for object in output.contents() { + println!(" - {}", object.key().unwrap_or("Unknown")); + } + } + Err(err) => { + eprintln!("{err:?}") + } + } } Ok(())