Skip to content

Commit 8ec038c

Browse files
authored
Use common prefixes for logstream listing (#69)
Fixes log stream listing by using common prefixes. https://docs.rs/aws-sdk-s3/latest/aws_sdk_s3/output/struct.ListObjectsV2Output.html#method.common_prefixes This lists all the root level objects which in case of parseable are log stream prefixes. Fixes #51
1 parent 3f034de commit 8ec038c

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

server/src/s3.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use async_trait::async_trait;
22
use aws_sdk_s3::error::{HeadBucketError, HeadBucketErrorKind};
3-
use aws_sdk_s3::model::{Delete, ObjectIdentifier};
3+
use aws_sdk_s3::model::{CommonPrefix, Delete, ObjectIdentifier};
44
use aws_sdk_s3::types::{ByteStream, SdkError};
55
use aws_sdk_s3::Error as AwsSdkError;
66
use aws_sdk_s3::{Client, Credentials, Endpoint, Region};
@@ -18,7 +18,6 @@ use futures::StreamExt;
1818
use http::Uri;
1919
use object_store::aws::AmazonS3Builder;
2020
use object_store::limit::LimitStore;
21-
use std::collections::HashSet;
2221
use std::fs;
2322
use std::iter::Iterator;
2423
use std::sync::Arc;
@@ -298,23 +297,22 @@ impl S3 {
298297
.client
299298
.list_objects_v2()
300299
.bucket(&S3_CONFIG.s3_bucket_name)
300+
.delimiter('/')
301301
.send()
302302
.await?;
303-
let body = resp.contents().unwrap_or_default();
304-
// make a set of unique prefixes at the root level
305-
let mut hs = HashSet::<String>::new();
306-
for logstream in body {
307-
let name = logstream.key().unwrap_or_default().to_string();
308-
let tokens = name.split('/').collect::<Vec<&str>>();
309-
hs.insert(tokens[0].to_string());
310-
}
311-
// transform that hashset to a vector before returning
312-
let mut streams = Vec::new();
313-
for v in hs {
314-
streams.push(LogStream { name: v });
315-
}
316303

317-
Ok(streams)
304+
let common_prefixes = resp.common_prefixes().unwrap_or_default();
305+
306+
// return prefixes at the root level
307+
let logstreams: Vec<_> = common_prefixes
308+
.iter()
309+
.filter_map(CommonPrefix::prefix)
310+
.filter_map(|name| name.strip_suffix('/'))
311+
.map(String::from)
312+
.map(|name| LogStream { name })
313+
.collect();
314+
315+
Ok(logstreams)
318316
}
319317

320318
async fn _upload_file(&self, key: &str, path: &str) -> Result<(), AwsSdkError> {

0 commit comments

Comments
 (0)