Crate obs_sdk

Crate obs_sdk 

Source
Expand description

§使用说明

§1. 列举桶内对象列表

use obs_sdk::ObsClient;
 
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_list_prefix() -> Result<(), Box<dyn std::error::Error>> {
    let client = ObsClient {
        endpoint: ENDPOINT.to_string(),
        ak: AK.to_string(),
        sk: SK.to_string(),
        bucket: BUCKET_NAME.to_string(),
    };
    let res = client.list("tmp").await?;
    println!("{:?}", res);
    Ok(())
}

§2. 上传对象到桶

use obs_sdk::ObsClient;
 
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_upload_object() -> Result<(), Box<dyn std::error::Error>> {
    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(())
}

§3. 下载对象到本地目录

use obs_sdk::ObsClient;
 
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_file02() -> Result<(), Box<dyn std::error::Error>> {
    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
}

§4. 下载对象为字节内容

use obs_sdk::ObsClient;
 
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<(), Box<dyn std::error::Error>> {
    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(())
}

§5. url鉴权

use obs_sdk::ObsClient;

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";
 
#[test]
fn test_url_sign() -> Result<(), Box<dyn std::error::Error>> {
    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(())
}

Structs§

ObjectMeta
obs对象的元数据信息
ObsClient
华为云OBS客户端