u-sdk 0.1.0

SDKs for some applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! 只实现了小部分API
//!
//! [阿里云API文档](https://help.aliyun.com/zh/oss/developer-reference/bucket-operations/)

use super::OSSClient;
use super::sign_v4::{HTTPVerb, SignV4Param};
use super::utils::{SerializeToHashMap, handle_response_status, into_request_header};
use crate::error::Error;
use crate::utils::common::gmt_format;

use serde::{Deserialize, Serialize};
use serde_with::{DisplayFromStr, serde_as};
use std::collections::{BTreeMap, BTreeSet, HashMap};
use url::Url;

// region:    --- put bucket
#[serde_with::skip_serializing_none]
#[derive(Serialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct PutBucketHeader<'a> {
    /// 为`None`时该请求头值默认为`private`
    pub x_oss_acl: Option<&'a str>,
    pub x_oss_resource_group_id: Option<&'a str>,
}

impl SerializeToHashMap for PutBucketHeader<'_> {}

#[serde_with::skip_serializing_none]
#[derive(Serialize, Default)]
#[serde(rename_all = "PascalCase")]
pub struct CreateBucketConfiguration<'a> {
    /// 默认为`Standard`
    pub storage_class: Option<&'a str>,
    /// 默认为`LRS`
    pub data_redundancy_type: Option<&'a str>,
}
// endregion: --- put bucket

// region:    --- list objects v2
/// `list-type`将自动设为2
#[serde_as]
#[serde_with::skip_serializing_none]
#[derive(Serialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct ListObjectsV2Query<'a> {
    // list-type: 2
    pub delimiter: Option<&'a str>,
    pub start_after: Option<&'a str>,
    pub continuation_token: Option<&'a str>,
    #[serde_as(as = "Option<DisplayFromStr>")]
    pub max_keys: Option<u16>,
    pub prefix: Option<&'a str>,
    pub encoding_type: Option<&'a str>,
    #[serde_as(as = "Option<DisplayFromStr>")]
    pub fetch_owner: Option<bool>,
}
impl SerializeToHashMap for ListObjectsV2Query<'_> {}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct ListBucketResult {
    pub contents: Option<Vec<Content>>,
    pub common_prefixes: Option<CommonPrefixes>,
    pub delimiter: String,
    pub encoding_type: Option<String>,
    pub is_truncated: bool,
    pub start_after: Option<String>,
    pub max_keys: u16,
    pub name: String,
    pub prefix: String,
    pub continuation_token: Option<u32>,
    pub key_count: u32,
    pub next_continuation_token: Option<String>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct CommonPrefixes {
    pub prefix: String,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct Content {
    pub owner: Option<Owner>,
    pub e_tag: String,
    pub key: String,
    pub last_modified: String,
    pub size: u32,
    pub storage_class: String,
    pub restore_info: Option<String>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct Owner {
    pub display_name: String,
    #[serde(rename = "ID")]
    pub id: String,
}
// endregion: --- list objects v2

// region:    --- get bucket info
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct BucketInfo {
    pub bucket: Bucket,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct Bucket {
    pub creation_date: String,
    pub extranet_endpoint: String,
    pub intranet_endpoint: String,
    pub location: String,
    pub storage_class: String,
    pub name: String,
    pub resource_group_id: String,
    pub owner: Owner,
    pub access_control_list: AccessControlList,
    pub data_redundancy_type: String,
    pub cross_region_replication: String,
    pub transfer_acceleration: String,
    pub access_monitor: String,
    pub bucket_policy: BucketPolicy,
    pub comment: String,
    pub server_side_encryption_rule: ServerSideEncryptionRule,
}

#[derive(Deserialize, Debug)]
pub struct ServerSideEncryptionRule {
    #[serde(rename = "SSEAlgorithm")]
    pub ssealgorithm: String,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct AccessControlList {
    pub grant: String,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct BucketPolicy {
    pub log_bucket: String,
    pub log_prefix: String,
}
// endregion: --- get bucket info

// xml数据为:"<LocationConstraint>oss-cn-hangzhou</LocationConstraint>",
// 这种情况下使用xml反序列化比较特殊,写法得类似于下面这样:
#[derive(Deserialize)]
struct LocationConstraint {
    #[serde(rename = "$text")]
    pub field: String,
}

// region:    --- get bucket stat
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct BucketStat {
    pub storage: u64,
    pub object_count: u32,
    pub multipart_upload_count: u32,
    pub live_channel_count: u32,
    pub last_modified_time: u64,
    pub standard_storage: u64,
    pub standard_object_count: u32,
    pub infrequent_access_storage: u64,
    pub infrequent_access_real_storage: u64,
    pub infrequent_access_object_count: u32,
    pub archive_storage: u64,
    pub archive_real_storage: u64,
    pub archive_object_count: u32,
    pub cold_archive_storage: u64,
    pub cold_archive_real_storage: u64,
    pub cold_archive_object_count: u32,
}
// endregion: --- get bucket stat

/// bucket相关API
impl OSSClient {
    pub async fn put_bucket(
        &self,
        bucket_name: &str,
        endpoint: &str,
        x_header: Option<PutBucketHeader<'_>>,
        bucket_conf: Option<CreateBucketConfiguration<'_>>,
    ) -> Result<(), Error> {
        let request_url = Url::parse(&format!("https://{}.{}", bucket_name, endpoint)).unwrap();
        let mut canonical_header = BTreeMap::new();
        let put_bucket_map = if let Some(h) = x_header {
            h.serialize_to_hashmap()?
        } else {
            HashMap::with_capacity(0)
        };
        canonical_header.extend(put_bucket_map.iter().map(|(k, v)| (k.as_str(), v.as_str())));
        canonical_header.insert("x-oss-content-sha256", "UNSIGNED-PAYLOAD");
        canonical_header.insert("host", request_url.host_str().unwrap());

        let mut additional_header = BTreeSet::new();
        additional_header.insert("host");
        let now = time::OffsetDateTime::now_utc();
        let sign_v4_param = SignV4Param {
            signing_region: &self.region,
            http_verb: HTTPVerb::Put,
            uri: &request_url,
            bucket: Some(bucket_name),
            header_map: &canonical_header,
            additional_header: Some(&additional_header),
            date_time: &now,
        };
        let authorization = self.sign_v4(sign_v4_param);

        let mut header = canonical_header.into_iter().collect::<HashMap<_, _>>();
        header.insert("Authorization", &authorization);
        let gmt = gmt_format(&now);
        header.insert("Date", &gmt);
        let header_map = into_request_header(header);

        let rq_xml = quick_xml::se::to_string(&bucket_conf)
            .map_err(|_| Error::AnyError("failed to serialize bucket configuration".to_owned()))?;
        // println!("rq_xml: {}", rq_xml);
        let resp = self
            .http_client
            .put(request_url)
            .headers(header_map)
            .body(rq_xml)
            .send()
            .await?;
        let _ = handle_response_status(resp).await?;

        Ok(())
    }

    pub async fn list_objects_v2(
        &self,
        query_params: ListObjectsV2Query<'_>,
    ) -> Result<ListBucketResult, Error> {
        let mut query_map = query_params.serialize_to_hashmap()?;

        // 添加固定的query参数
        query_map.insert("list-type".to_owned(), "2".to_owned());
        let sign_url = Url::parse_with_params(
            &format!("https://{}.{}/", self.bucket, self.endpoint),
            query_map,
        )
        .unwrap();

        let mut canonical_header = BTreeMap::new();
        canonical_header.insert("x-oss-content-sha256", "UNSIGNED-PAYLOAD");
        canonical_header.insert("host", sign_url.host_str().unwrap());

        let mut additional_header = BTreeSet::new();
        additional_header.insert("host");
        let now = time::OffsetDateTime::now_utc();
        let sign_v4_param = SignV4Param {
            signing_region: &self.region,
            http_verb: HTTPVerb::Get,
            uri: &sign_url,
            bucket: Some(&self.bucket),
            header_map: &canonical_header,
            additional_header: Some(&additional_header),
            date_time: &now,
        };
        let authorization = self.sign_v4(sign_v4_param);

        let mut header = canonical_header.into_iter().collect::<HashMap<_, _>>();
        header.insert("Authorization", &authorization);
        let gmt = gmt_format(&now);
        header.insert("Date", &gmt);
        let header_map = into_request_header(header);

        let resp = self
            .http_client
            .get(sign_url)
            .headers(header_map)
            .send()
            .await?;

        let text = handle_response_status(resp).await?;
        // println!("text: {}", text);
        let res = quick_xml::de::from_str(&text)?;

        Ok(res)
    }

    /// - `bucket`: 如果为`None`,则获取[`OSSClient`]中的`bucket`信息,否则获取指定的`bucket`的信息
    pub async fn get_bucket_info(&self) -> Result<BucketInfo, Error> {
        let request_url = Url::parse_with_params(
            &format!("https://{}.{}", self.bucket, self.endpoint),
            [("bucketInfo", "")],
        )
        .unwrap();
        let mut canonical_header = BTreeMap::new();
        canonical_header.insert("x-oss-content-sha256", "UNSIGNED-PAYLOAD");
        canonical_header.insert("host", request_url.host_str().unwrap());
        let mut additional_header = BTreeSet::new();
        additional_header.insert("host");
        let now = time::OffsetDateTime::now_utc();
        let sign_v4_param = SignV4Param {
            signing_region: &self.region,
            http_verb: HTTPVerb::Get,
            uri: &request_url,
            bucket: Some(&self.bucket),
            header_map: &canonical_header,
            additional_header: Some(&additional_header),
            date_time: &now,
        };
        let auth = self.sign_v4(sign_v4_param);
        let mut header = canonical_header.into_iter().collect::<HashMap<_, _>>();
        header.insert("Authorization", &auth);
        let gmt = gmt_format(&now);
        header.insert("Date", &gmt);

        let header_map = into_request_header(header);
        let resp = self
            .http_client
            .get(request_url)
            .headers(header_map)
            .send()
            .await?;

        let text = handle_response_status(resp).await?;
        let res = quick_xml::de::from_str(&text)?;

        Ok(res)
    }

    /// - `other_bucket`: 如果为`None`,则获取[`OSSClient`]中的`bucket`信息,否则获取`other_bucket`的信息
    pub async fn get_bucket_location(&self) -> Result<String, Error> {
        let request_url = Url::parse_with_params(
            &format!("https://{}.{}", self.bucket, self.endpoint),
            [("location", "")],
        )
        .unwrap();
        let mut canonical_header = BTreeMap::new();
        canonical_header.insert("x-oss-content-sha256", "UNSIGNED-PAYLOAD");
        canonical_header.insert("host", request_url.host_str().unwrap());
        let mut additional_header = BTreeSet::new();
        additional_header.insert("host");
        let now = time::OffsetDateTime::now_utc();
        let sign_v4_param = SignV4Param {
            signing_region: &self.region,
            http_verb: HTTPVerb::Get,
            uri: &request_url,
            bucket: Some(&self.bucket),
            header_map: &canonical_header,
            additional_header: Some(&additional_header),
            date_time: &now,
        };
        let auth = self.sign_v4(sign_v4_param);
        let mut header = canonical_header.into_iter().collect::<HashMap<_, _>>();
        header.insert("Authorization", &auth);
        let gmt = gmt_format(&now);
        header.insert("Date", &gmt);

        let header_map = into_request_header(header);
        let resp = self
            .http_client
            .get(request_url)
            .headers(header_map)
            .send()
            .await?;

        let text = handle_response_status(resp).await?;
        let res: LocationConstraint = quick_xml::de::from_str(&text)?;
        Ok(res.field)
    }

    /// - `other_bucket`: 如果为`None`,则获取[`OSSClient`]中的`bucket`信息,否则获取`other_bucket`的信息
    pub async fn get_bucket_stat(&self) -> Result<BucketStat, Error> {
        let request_url = Url::parse_with_params(
            &format!("https://{}.{}", self.bucket, self.endpoint),
            [("stat", "")],
        )
        .unwrap();
        let mut canonical_header = BTreeMap::new();
        canonical_header.insert("x-oss-content-sha256", "UNSIGNED-PAYLOAD");
        canonical_header.insert("host", request_url.host_str().unwrap());
        let mut additional_header = BTreeSet::new();
        additional_header.insert("host");
        let now = time::OffsetDateTime::now_utc();
        let sign_v4_param = SignV4Param {
            signing_region: &self.region,
            http_verb: HTTPVerb::Get,
            uri: &request_url,
            bucket: Some(&self.bucket),
            header_map: &canonical_header,
            additional_header: Some(&additional_header),
            date_time: &now,
        };
        let auth = self.sign_v4(sign_v4_param);
        let mut header = canonical_header.into_iter().collect::<HashMap<_, _>>();
        header.insert("Authorization", &auth);
        let gmt = gmt_format(&now);
        header.insert("Date", &gmt);
        let header_map = into_request_header(header);

        let resp = self
            .http_client
            .get(request_url)
            .headers(header_map)
            .send()
            .await?;

        let text = handle_response_status(resp).await?;
        let res = quick_xml::de::from_str(&text)?;

        Ok(res)
    }
}