# obs-sdk

## 介绍

针对华为云的OBS工具封装rust版本的sdk

华为obs文档：https://support.huaweicloud.com/api-obs/obs_04_0010.html

在线签名工具：https://obs-community.obs.cn-north-1.myhuaweicloud.com/sign/header_signature.html

目前已实现的API：

- 桶内对象列举
- 上传obs对象
- 下载obs对象
- header中携带签名
- url中携带签名

## 软件架构

软件架构说明


## 安装教程

1. 依赖安装

```bash
cargo add reqwest -F "multipart,json"
cargo add tokio -F "full"
cargo add serde -F "derive"
cargo add serde_yaml serde_derive serde_json hmac sha1 base64 chrono regex md5 
```


## 使用说明

### 安装

```bash
cargo add obs-sdk
```

### 1. 列举桶内对象列表

```rust
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. 上传对象到桶

```rust
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. 下载对象到本地目录

```rust
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. 下载对象为字节内容

```rust
use obs_sdk::ObsClient;
use std::path::Path;
use std::fs;

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鉴权

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

#### 参与贡献

1.  Fork 本仓库
2.  新建 Feat_xxx 分支
3.  提交代码
4.  新建 Pull Request


#### 特技

1.  使用 Readme\_XXX.md 来支持不同的语言，例如 Readme\_en.md, Readme\_zh.md
2.  Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com)
3.  你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目
4.  [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目，是综合评定出的优秀开源项目
5.  Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
6.  Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
