download works

This commit is contained in:
2026-01-03 19:03:39 +01:00
parent da499c792f
commit 135df74b1b
5 changed files with 20 additions and 13 deletions
+3
View File
@@ -1 +1,4 @@
/target /target
/result
/.vscode
+2 -2
View File
@@ -4,7 +4,7 @@ mod utils;
use models::CloudS3; use models::CloudS3;
use std::fs; use std::fs;
use utils::print_bucket_contents; use utils::handle_bucket_contents;
use crate::utils::s3_utils::{create_client, create_config}; use crate::utils::s3_utils::{create_client, create_config};
@@ -24,7 +24,7 @@ async fn main() {
println!("Bucket tartalom listázása: {}", cloud.name); println!("Bucket tartalom listázása: {}", cloud.name);
match print_bucket_contents(&client, &cloud).await { match handle_bucket_contents(&client, &cloud).await {
Ok(_) => (), Ok(_) => (),
Err(e) => eprintln!("Hiba a bucket listázásakor: {}", e), Err(e) => eprintln!("Hiba a bucket listázásakor: {}", e),
} }
+8 -1
View File
@@ -9,6 +9,7 @@ pub fn create_directory_if_not_exists(folder_name: &str) -> std::io::Result<()>
} }
pub fn create_file_if_not_exist_or_differ( pub fn create_file_if_not_exist_or_differ(
root_folder: &str,
bucket_item: &S3Item, bucket_item: &S3Item,
content: &[u8], content: &[u8],
) -> std::io::Result<()> { ) -> std::io::Result<()> {
@@ -16,7 +17,9 @@ pub fn create_file_if_not_exist_or_differ(
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
let path = Path::new(bucket_item.key.as_str()); let path_str = format!("{}/{}", root_folder, bucket_item.key);
let path = Path::new(&path_str);
if path.exists() if path.exists()
&& bucket_item && bucket_item
@@ -26,6 +29,10 @@ pub fn create_file_if_not_exist_or_differ(
return Ok(()); return Ok(());
} }
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
// Ha a fájl nem létezik vagy a tartalom különbözik, írjuk a fájlt // Ha a fájl nem létezik vagy a tartalom különbözik, írjuk a fájlt
let mut file = fs::File::create(path)?; let mut file = fs::File::create(path)?;
file.write_all(content)?; file.write_all(content)?;
+1 -3
View File
@@ -1,5 +1,3 @@
pub mod file_utils; pub mod file_utils;
pub mod s3_utils; pub mod s3_utils;
pub use file_utils::create_directory_if_not_exists; pub use s3_utils::handle_bucket_contents;
pub use s3_utils::print_bucket_contents;
+6 -7
View File
@@ -1,10 +1,8 @@
use super::file_utils::create_directory_if_not_exists; use super::file_utils::create_file_if_not_exist_or_differ;
use crate::models::CloudS3; use crate::models::CloudS3;
use crate::models::S3Item; use crate::models::S3Item;
use crate::utils::file_utils::create_file_if_not_exist_or_differ;
use aws_config::BehaviorVersion; use aws_config::BehaviorVersion;
use aws_sdk_s3::config::{Credentials, Region}; use aws_sdk_s3::config::{Credentials, Region};
use aws_sdk_s3::operation::get_object::GetObjectOutput;
use aws_sdk_s3::{Client, Error}; use aws_sdk_s3::{Client, Error};
pub fn create_config(s3: &CloudS3) -> aws_sdk_s3::Config { pub fn create_config(s3: &CloudS3) -> aws_sdk_s3::Config {
@@ -119,7 +117,7 @@ pub async fn download_file(
} }
/// Egyszerű listázó függvény, ami kiírja a tartalmat /// Egyszerű listázó függvény, ami kiírja a tartalmat
pub async fn print_bucket_contents( pub async fn handle_bucket_contents(
client: &Client, client: &Client,
s3: &CloudS3, s3: &CloudS3,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
@@ -131,13 +129,14 @@ pub async fn print_bucket_contents(
} }
println!("{}", "-".repeat(60)); println!("{}", "-".repeat(60));
let root_folder = format!("./result/{}", s3.name);
for item in &items { for item in &items {
println!("{}", item); print!("\r{}", item.key);
let content = download_file(client, &s3, &item).await?; let content = download_file(client, &s3, &item).await?;
create_directory_if_not_exists(item.folder_name.as_deref().unwrap_or("")); create_file_if_not_exist_or_differ(&root_folder, &item, &content);
create_file_if_not_exist_or_differ(item, &content);
} }
println!("Contents count: {}", items.len()); println!("Contents count: {}", items.len());