Skip to main content

Crate obs_sdk

Crate obs_sdk 

Source
Expand description

§使用说明

§1. 列举桶内对象列表

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_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("tmp", false).await?;
    println!("{:?}", res);
    Ok(())
}

§2. 上传对象到桶

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_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(())
}

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

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_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
}

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

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(())
}

§5. url鉴权

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";
 
#[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(())
}

§6. 批量上传文件

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_upload_files() -> Result<(), ObsError> {
    let client = ObsClient {
        endpoint: ENDPOINT.to_string(),
        ak: AK.to_string(),
        sk: SK.to_string(),
        bucket: BUCKET_NAME.to_string(),
    };
    let files = vec![
        ("file1.txt", "local/path/file1.txt"),
        ("file2.txt", "local/path/file2.txt"),
        ("file3.txt", "local/path/file3.txt"),
    ];
    let results = client.upload_files(files).await;
    for (i, result) in results.iter().enumerate() {
        match result {
            Ok(_) => println!("文件{}上传成功", i),
            Err(e) => eprintln!("文件{}上传失败: {:?}", i, e),
        }
    }
    Ok(())
}

§7. 批量下载文件

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_files() -> Result<(), ObsError> {
    let client = ObsClient {
        endpoint: ENDPOINT.to_string(),
        ak: AK.to_string(),
        sk: SK.to_string(),
        bucket: BUCKET_NAME.to_string(),
    };
    let files = vec![
        ("remote/file1.txt", "local/path/file1.txt", false),
        ("remote/file2.txt", "local/path/file2.txt", true),
        ("remote/file3.txt", "local/path/file3.txt", false),
    ];
    let results = client.download_files(files).await;
    for (i, result) in results.iter().enumerate() {
        match result {
            Ok(_) => println!("文件{}下载成功", i),
            Err(e) => eprintln!("文件{}下载失败: {:?}", i, e),
        }
    }
    Ok(())
}

Structs§

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

Enums§

ObsError