use std::fs;
use std::path::Path;
use obs_sdk::{ObsClient, ObsError};
static ENDPOINT: &str = "obs.cn-north-4.myhuaweicloud.com";
static AK: &str = "YOUR_AK";
static SK: &str = "YOUR_SK";
static BUCKET_NAME: &str = "bucket_name";
#[tokio::test]
async fn test_download_file01() -> Result<(), ObsError> {
let client = ObsClient {
endpoint: ENDPOINT.to_string(),
ak: AK.to_string(),
sk: SK.to_string(),
bucket: BUCKET_NAME.to_string(),
};
let data = client.download_object("2hls_stutter-10.mp4").await?;
let file_path = Path::new("output.mp4");
match fs::write(file_path, data) {
Ok(_) => println!("文件保存成功{:?}", file_path),
Err(e) => eprintln!("文件保存失败:{}", e)
}
Ok(())
}
#[tokio::test]
async fn test_download_file02() -> Result<(), ObsError> {
let client = ObsClient {
endpoint: ENDPOINT.to_string(),
ak: AK.to_string(),
sk: SK.to_string(),
bucket: BUCKET_NAME.to_string(),
};
let res = client.download_file("2hls_stutter-10.mp4", "video/2hls_stutter-10.mp4", false).await;
res
}
#[tokio::test]
async fn test_download_files() -> Result<(), ObsError> {
let client = ObsClient {
endpoint: ENDPOINT.to_string(),
ak: AK.to_string(),
sk: SK.to_string(),
bucket: BUCKET_NAME.to_string(),
};
let res = client.list("sync", true).await?;
let temp_paths: Vec<String> = res
.iter()
.map(|meta| meta.key.replacen("sync/", "D:/tmp/obs/", 1))
.collect();
let download_files: Vec<(String, String, bool)> = res
.iter()
.zip(temp_paths.iter())
.map(|(meta, path)| (meta.key.clone(), path.clone(), true))
.collect();
client.download_files(download_files).await;
Ok(())
}
#[tokio::test]
async fn test_list_prefix() -> Result<(), ObsError> {
let client = ObsClient {
endpoint: ENDPOINT.to_string(),
ak: AK.to_string(),
sk: SK.to_string(),
bucket: BUCKET_NAME.to_string(),
};
let res = client.list("sync", true).await?;
println!("{:?}", res.len());
Ok(())
}
#[tokio::test]
async fn test_upload_object() -> Result<(), ObsError> {
let client = ObsClient {
endpoint: ENDPOINT.to_string(),
ak: AK.to_string(),
sk: SK.to_string(),
bucket: BUCKET_NAME.to_string(),
};
let res = client.upload_file("tmp_cargo.txt", "Cargo.txt").await?;
println!("{:?}", res);
Ok(())
}
#[tokio::test]
async fn test_delete_object() -> Result<(), ObsError> {
let client = ObsClient {
endpoint: ENDPOINT.to_string(),
ak: AK.to_string(),
sk: SK.to_string(),
bucket: BUCKET_NAME.to_string(),
};
let res = client.delete_object("remote1/.operate/00000000000000_20251017161709.txt").await?;
println!("{:?}", res);
Ok(())
}
#[test]
fn test_url_sign() -> Result<(), ObsError> {
let client = ObsClient {
endpoint: ENDPOINT.to_string(),
ak: AK.to_string(),
sk: SK.to_string(),
bucket: BUCKET_NAME.to_string(),
};
let sign_url = client.url_sign("https://ranfs.obs.cn-north-4.myhuaweicloud.com/tmp_cargo.txt")?;
println!("sign_url = {}", sign_url);
Ok(())
}
#[test]
fn test_mime_type() {
let guess = mime_guess::from_path("a/b/c/some_file.dat");
println!("mime_type = {:?}", guess.first().unwrap().to_string());
}