@@ -27,11 +27,11 @@ pub async fn upload_to_s3(
|
||||
|
||||
/// Проверяет, существует ли файл в S3.
|
||||
pub async fn check_file_exists(
|
||||
storj_client: &S3Client,
|
||||
s3_client: &S3Client,
|
||||
bucket: &str,
|
||||
file_key: &str,
|
||||
filepath: &str,
|
||||
) -> Result<bool, actix_web::Error> {
|
||||
match storj_client.head_object().bucket(bucket).key(file_key).send().await {
|
||||
match s3_client.head_object().bucket(bucket).key(filepath).send().await {
|
||||
Ok(_) => Ok(true), // Файл найден
|
||||
Err(SdkError::ServiceError(service_error)) if service_error.err().is_not_found() => {
|
||||
Ok(false) // Файл не найден
|
||||
@@ -74,3 +74,34 @@ pub fn generate_key_with_extension(base_key: String, mime_type: String) -> Strin
|
||||
}
|
||||
base_key
|
||||
}
|
||||
|
||||
/// список файлов из S3
|
||||
pub async fn get_s3_filelist(client: &S3Client, bucket: &str) -> Vec<[std::string::String; 2]> {
|
||||
let mut filekeys = Vec::new();
|
||||
// Запрашиваем список файлов из S3
|
||||
let list_objects_v2 = client.list_objects_v2();
|
||||
let list_response = list_objects_v2
|
||||
.bucket(bucket)
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to list files from Storj");
|
||||
|
||||
if let Some(objects) = list_response.contents {
|
||||
for object in objects.iter() {
|
||||
if let Some(s3_filepath) = &object.key {
|
||||
let filepath = match s3_filepath.ends_with("/webp") {
|
||||
true => &s3_filepath.replace("/webp", ""),
|
||||
false => s3_filepath,
|
||||
};
|
||||
let mut parts = filepath.split('/').collect::<Vec<&str>>(); // Explicit type annotation
|
||||
let filename = parts.pop().unwrap();
|
||||
let mut filename_parts = filename.split('.').collect::<Vec<&str>>();
|
||||
let _ext = filename_parts.pop().unwrap();
|
||||
let filekey = filename_parts.pop().unwrap();
|
||||
|
||||
filekeys.push([filekey.to_string(), s3_filepath.to_string()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
filekeys
|
||||
}
|
||||
Reference in New Issue
Block a user