create folder but does not work yet
This commit is contained in:
@@ -1,2 +1,6 @@
|
|||||||
pub mod cloud;
|
pub mod cloud;
|
||||||
|
pub mod s3_items;
|
||||||
|
|
||||||
pub use cloud::CloudS3;
|
pub use cloud::CloudS3;
|
||||||
|
pub use s3_items::S3Item;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
pub struct S3Item {
|
||||||
|
pub key: String,
|
||||||
|
pub is_folder: bool,
|
||||||
|
pub folder_name: Option<String>,
|
||||||
|
pub size: Option<i64>,
|
||||||
|
pub last_modified: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for S3Item {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let item_type = if self.is_folder { "[MAPPA]" } else { "[FÁJL]" };
|
||||||
|
let size_str = if let Some(size) = self.size {
|
||||||
|
format!("{} bytes", size)
|
||||||
|
} else {
|
||||||
|
"-".to_string()
|
||||||
|
};
|
||||||
|
write!(f, "{} {} (méret: {})", item_type, self.key, size_str)
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
-20
@@ -1,29 +1,10 @@
|
|||||||
use crate::models::CloudS3;
|
use crate::models::CloudS3;
|
||||||
|
use crate::models::S3Item;
|
||||||
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::operation::get_object::GetObjectOutput;
|
||||||
use aws_sdk_s3::{Client, Error};
|
use aws_sdk_s3::{Client, Error};
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct S3Item {
|
|
||||||
pub key: String,
|
|
||||||
pub is_folder: bool,
|
|
||||||
pub size: Option<i64>,
|
|
||||||
pub last_modified: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for S3Item {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let item_type = if self.is_folder { "[MAPPA]" } else { "[FÁJL]" };
|
|
||||||
let size_str = if let Some(size) = self.size {
|
|
||||||
format!("{} bytes", size)
|
|
||||||
} else {
|
|
||||||
"-".to_string()
|
|
||||||
};
|
|
||||||
write!(f, "{} {} (méret: {})", item_type, self.key, size_str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_config(s3: &CloudS3) -> aws_sdk_s3::Config {
|
pub fn create_config(s3: &CloudS3) -> aws_sdk_s3::Config {
|
||||||
let credentials = Credentials::new(&s3.accesskey, &s3.secretekey, None, None, "cloudbackup");
|
let credentials = Credentials::new(&s3.accesskey, &s3.secretekey, None, None, "cloudbackup");
|
||||||
|
|
||||||
@@ -78,6 +59,7 @@ pub async fn list_bucket(client: &Client, s3: &CloudS3) -> Result<Vec<S3Item>, E
|
|||||||
key: prefix_str.to_string(),
|
key: prefix_str.to_string(),
|
||||||
is_folder: true,
|
is_folder: true,
|
||||||
size: None,
|
size: None,
|
||||||
|
folder_name: Some(prefix_str.to_string()),
|
||||||
last_modified: None,
|
last_modified: None,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -88,12 +70,20 @@ pub async fn list_bucket(client: &Client, s3: &CloudS3) -> Result<Vec<S3Item>, E
|
|||||||
if let Some(key) = object.key() {
|
if let Some(key) = object.key() {
|
||||||
// Kihagyjuk a mappa neveket (amik '/' karakterrel végződnek)
|
// Kihagyjuk a mappa neveket (amik '/' karakterrel végződnek)
|
||||||
if !key.ends_with('/') {
|
if !key.ends_with('/') {
|
||||||
|
let folder_name = if let Some(pos) = key.rfind('/') {
|
||||||
|
Some(key[..pos].to_string())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
println!("Folder neve: {}", folder_name.as_deref().unwrap_or("N/A"));
|
||||||
items.push(S3Item {
|
items.push(S3Item {
|
||||||
key: key.to_string(),
|
key: key.to_string(),
|
||||||
is_folder: false,
|
is_folder: false,
|
||||||
|
folder_name: folder_name.clone(),
|
||||||
size: object.size(),
|
size: object.size(),
|
||||||
last_modified: object.last_modified().map(|dt| dt.to_string()),
|
last_modified: object.last_modified().map(|dt| dt.to_string()),
|
||||||
});
|
});
|
||||||
|
create_directory_if_not_exists(folder_name.as_deref().unwrap_or(""));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -143,3 +133,11 @@ pub async fn print_bucket_contents(client: &Client, s3: &CloudS3) -> Result<(),
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn create_directory_if_not_exists(folder_name: &str) -> std::io::Result<()> {
|
||||||
|
let folder_name_with_root = "./result".to_string() + "/" + folder_name;
|
||||||
|
if !std::path::Path::new(&folder_name_with_root).exists() {
|
||||||
|
std::fs::create_dir_all(folder_name_with_root)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user