Module file

Source
Expand description

§OSS 文件相关操作

File 是一个文件操作的工具包,包含上传,下载,删除功能,开发者可以方便的调用使用

use std::{fs, io, path::Path};

use aliyun_oss_client::{
    config::get_url_resource,
    errors::OssError,
    file::{File, FileError, GetStd},
    types::CanonicalizedResource,
    BucketName, Client, EndPoint, KeyId, KeySecret,
};
use reqwest::Url;

struct MyObject {
    path: String,
}

impl MyObject {
    const KEY_ID: KeyId = KeyId::from_static("xxxxxxxxxxxxxxxx");
    const KEY_SECRET: KeySecret = KeySecret::from_static("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    const END_POINT: EndPoint = EndPoint::SHANGHAI;
    const BUCKET: BucketName = unsafe { BucketName::from_static2("xxxxxx") };

    fn new(path: &Path) -> Result<MyObject, io::Error> {
        Ok(MyObject {
            path: path.to_str().unwrap().to_owned(),
        })
    }
}

impl GetStd for MyObject {
    fn get_std(&self) -> Option<(Url, CanonicalizedResource)> {
        let path = self.path.clone().try_into().unwrap();
        Some(get_url_resource(&Self::END_POINT, &Self::BUCKET, &path))
    }
}

impl File<Client> for MyObject {
    fn oss_client(&self) -> Client {
        Client::new(
            Self::KEY_ID,
            Self::KEY_SECRET,
            Self::END_POINT,
            Self::BUCKET,
        )
    }
}

async fn run() -> Result<(), OssError> {
    for entry in fs::read_dir("examples")? {
        let path = entry?.path();
        let path = path.as_path();

        if !path.is_file() {
            continue;
        }

        let obj = MyObject::new(path)?;
        let content = fs::read(path)?;

        let res = obj
            .put_oss(content, "application/pdf")
            .await
            .map_err(OssError::from)?;

        println!("result status: {}", res.status());
    }

    Ok(())
}

Re-exports§

pub use blocking::Files as BlockingFiles;

Modules§

blocking
同步的文件模块

Structs§

FileError
文件模块的 Error

Constants§

DEFAULT_CONTENT_TYPE
默认 content-type

Traits§

AlignBuilder
对齐 ClientBucket, ObjectList 等结构体的 trait
File
文件的相关操作
Files
文件集合的相关操作
GetStd
获取请求 OSS 接口需要的信息
GetStdWithPath
根据给定路径,获取请求 OSS 接口需要的信息