object_storage/oss/
mod.rs

1pub mod object;
2pub mod service;
3pub mod util;
4use self::{
5    service::BucketInfo,
6    util::{crypto::content_md5, file::file_name},
7};
8use anyhow::{Ok, Result};
9use util::file::{file_type, read_file};
10#[derive(Debug)]
11pub struct Auth {
12    pub accesskeyid: String,
13    pub accesskeysecret: String,
14}
15
16impl Auth {
17    pub async fn new(accesskeyid: &str, accesskeysecret: &str) -> Self {
18        Auth {
19            accesskeyid: accesskeyid.to_owned(),
20            accesskeysecret: accesskeysecret.to_owned(),
21        }
22    }
23
24    pub async fn list_bucket(&self) -> Result<Vec<BucketInfo>> {
25        service::list_bucket(self).await
26    }
27}
28
29pub struct Bucket {
30    pub auth: Auth,
31    pub endpoint: String,
32    pub bucket_name: String,
33}
34
35impl Bucket {
36    pub async fn new(auth: Auth, endpoint: &str, bucket_name: &str) -> Result<Bucket> {
37        Ok(Bucket {
38            auth,
39            endpoint: endpoint.to_owned(),
40            bucket_name: bucket_name.to_owned(),
41        })
42    }
43}
44
45impl Bucket {
46    pub async fn read_put_object(&self, path: &str) -> Result<String> {
47        let file = read_file(path).await?;
48        let file_type = file_type(path).await?;
49        let file_name = file_name(path).await?;
50        let content = Content::new(&file, &file_type).await?;
51        let b = object::put_object(&self, &file, &content, &file_name).await?;
52        Ok(b)
53    }
54
55    pub async fn put_object(
56        &self,
57        file: Vec<u8>,
58        file_type: &str,
59        file_name: &str,
60    ) -> Result<String> {
61        let content = Content::new(&file, file_type).await?;
62        let b = object::put_object(&self, &file, &content, &file_name).await?;
63        Ok(b)
64    }
65}
66
67#[derive(Debug)]
68pub struct Content {
69    pub content_md5: String,
70    pub content_type: String,
71}
72
73impl Content {
74    pub async fn new(file: &[u8], file_type: &str) -> Result<Self> {
75        Ok(Content {
76            content_md5: content_md5(file).await?,
77            content_type: file_type.to_string(),
78        })
79    }
80}