Skip to content

Commit 6effea5

Browse files
authored
Merge pull request #9 from sangshuduo/feat/sangshuduo/random-pairs-with-s3
feat: support except file comand line argument
2 parents 609e57b + 536f4fb commit 6effea5

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

random_pairs_of_s3file/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
## random_pairs_of_s3file Usage:
22

3-
```shell
4-
Usage: random_pairs_of_s3file [OPTIONS] --num-pairs <NUM> --bucket <BUCKET> --directory <DIR> --url-prefix <PREFIX>
3+
Usage: random_pairs_of_s3file [OPTIONS] --num-pairs <NUM_PAIRS> --bucket <BUCKET> --directory <DIRECTORY> --url-prefix <URL_PREFIX>
54

65
Options:
7-
--num-pairs <NUM> Number of pairs to generate
8-
--bucket <BUCKET> Name of the S3 bucket
9-
--directory <DIR> Directory (prefix) in the bucket (e.g. "image/")
10-
--url-prefix <PREFIX> URL prefix for final URLs
11-
-h, --help Print help
12-
-V, --version Print version
6+
--num-pairs <NUM_PAIRS> Number of pairs to generate
7+
--bucket <BUCKET> Name of the S3 bucket
8+
--directory <DIRECTORY> Directory (prefix) in the bucket (e.g. "image/")
9+
--url-prefix <URL_PREFIX> URL prefix to form the final URL (e.g. "https://api.example.com/s3/api/v1/resource?url=s3://")
10+
--exclude-file <EXCLUDE_FILE> File containing keys to exclude
11+
-h, --help Print help
12+
-V, --version Print version

random_pairs_of_s3file/src/main.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use clap::Parser;
22
use rand::seq::SliceRandom;
33
use serde::Serialize;
4+
use std::collections::HashSet;
45
use std::error::Error;
6+
use std::fs::File;
7+
use std::io::{BufRead, BufReader};
58

69
// AWS SDK for Rust (1.x)
710
use aws_config::{load_defaults, BehaviorVersion};
@@ -28,6 +31,10 @@ struct Args {
2831
/// URL prefix to form the final URL (e.g. "https://api.example.com/s3/api/v1/resource?url=s3://")
2932
#[arg(long, required = true)]
3033
url_prefix: String,
34+
35+
/// File containing keys to exclude
36+
#[arg(long, required = false)]
37+
exclude_file: Option<String>,
3138
}
3239

3340
#[derive(Serialize)]
@@ -50,6 +57,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
5057
let directory_prefix = &args.directory;
5158
let url_prefix = &args.url_prefix;
5259

60+
// Read excluded keys from file if provided
61+
let excluded_keys: HashSet<String> = if let Some(exclude_file_path) = args.exclude_file {
62+
let file = File::open(&exclude_file_path)?;
63+
BufReader::new(file).lines().map_while(Result::ok).collect()
64+
} else {
65+
HashSet::new()
66+
};
67+
5368
let shared_config = load_defaults(BehaviorVersion::latest()).await;
5469
let s3_client = Client::new(&shared_config);
5570

@@ -77,6 +92,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
7792
let all_keys: Vec<String> = objects
7893
.iter()
7994
.filter_map(|obj| obj.key().map(str::to_string))
95+
.filter(|key| !excluded_keys.contains(key))
8096
.collect();
8197

8298
if all_keys.len() < 2 {
@@ -91,12 +107,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
91107
let mut all_pairs = Vec::new();
92108
for (i, source) in all_keys.iter().enumerate() {
93109
// check if source is empty
94-
if source.is_empty() {
110+
if source.is_empty() || source.ends_with('/') {
95111
continue;
96112
}
97113
for (j, candidate) in all_keys.iter().enumerate() {
98114
// check if candidate is is_empty
99-
if candidate.is_empty() {
115+
if candidate.is_empty() || candidate.ends_with('/') {
100116
continue;
101117
}
102118
if i != j {

0 commit comments

Comments
 (0)