u-sdk 0.6.1

Some useful SDKs
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! 只实现了小部分API
//!
//! [阿里云API文档](https://help.aliyun.com/zh/oss/developer-reference/bucket-operations/)

use super::Client;
use super::sign_v4::HTTPVerb;
use super::utils::{get_request_header, into_request_failed_error, parse_xml_response};
use crate::oss::Error;
use bon::Builder;
use serde::{Deserialize, Serialize};
use serde_with::{DisplayFromStr, serde_as};
use std::collections::HashMap;
use url::Url;

// region:    --- put bucket
#[serde_with::skip_serializing_none]
#[derive(Builder, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct PutBucket<'a> {
    #[builder(start_fn)]
    #[serde(skip_serializing)]
    pub(crate) client: &'a Client,
    #[serde(skip_serializing)]
    pub(crate) bucket_name: &'a str,
    // 请求参数
    #[serde(skip_serializing)]
    pub(crate) storage_class: Option<&'a str>,
    #[serde(skip_serializing)]
    pub(crate) data_redundancy_type: Option<&'a str>,
    // header
    x_oss_acl: Option<&'a str>,
    x_oss_resource_group_id: Option<&'a str>,
    x_oss_bucket_tagging: Option<&'a str>,
}

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

impl PutBucket<'_> {
    pub async fn send(&self) -> Result<(), Error> {
        let client = self.client;
        let request_url =
            Url::parse(&format!("https://{}.{}", self.bucket_name, client.endpoint)).unwrap();
        let mut req_header_map: HashMap<String, String> =
            serde_json::from_value(serde_json::to_value(self).unwrap()).unwrap();

        let creds = client.credentials_provider.load().await?;
        if let Some(token) = &creds.sts_security_token {
            req_header_map.insert("x-oss-security-token".to_string(), token.clone());
        }

        let header = get_request_header(
            &creds.access_key_id,
            &creds.access_key_secret,
            req_header_map,
            &request_url,
            HTTPVerb::Put,
            &client.region,
            Some(self.bucket_name),
        );

        let req_xml = {
            let create_conf = CreateBucketConfiguration {
                storage_class: self.storage_class,
                data_redundancy_type: self.data_redundancy_type,
            };

            quick_xml::se::to_string(&create_conf).unwrap()
        };

        let resp = client
            .http_client
            .put(request_url)
            .headers(header)
            .body(req_xml)
            .send()
            .await?;

        if !resp.status().is_success() {
            return Err(into_request_failed_error(resp).await);
        }

        Ok(())
    }
}
// endregion: --- put bucket

// region:    --- list objects v2
/// `list-type`将自动设为2
#[serde_as]
#[serde_with::skip_serializing_none]
#[derive(Builder, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct ListObjectsV2<'a> {
    #[builder(start_fn)]
    #[serde(skip_serializing)]
    pub(crate) client: &'a Client,
    // list-type: 2  固定的,自动添加
    delimiter: Option<&'a str>,
    start_after: Option<&'a str>,
    continuation_token: Option<&'a str>,
    #[serde_as(as = "Option<DisplayFromStr>")]
    max_keys: Option<u16>,
    prefix: Option<&'a str>,
    encoding_type: Option<&'a str>,
    #[serde_as(as = "Option<DisplayFromStr>")]
    fetch_owner: Option<bool>,
}

#[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>,
    pub r#type: String,
}

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

impl ListObjectsV2<'_> {
    pub async fn send(&self) -> Result<ListBucketResult, Error> {
        let mut query_map: HashMap<String, String> =
            serde_json::from_value(serde_json::to_value(self).unwrap()).unwrap();
        // 添加固定的query参数
        query_map.insert("list-type".to_owned(), "2".to_owned());

        let client = self.client;
        let sign_url = Url::parse_with_params(
            &format!("https://{}.{}/", client.bucket, client.endpoint),
            query_map,
        )
        .unwrap();

        let creds = client.credentials_provider.load().await?;
        let mut req_header_map = HashMap::new();
        if let Some(token) = &creds.sts_security_token {
            req_header_map.insert("x-oss-security-token".to_string(), token.clone());
        }

        let header = get_request_header(
            &creds.access_key_id,
            &creds.access_key_secret,
            req_header_map,
            &sign_url,
            HTTPVerb::Get,
            &client.region,
            Some(&client.bucket),
        );

        let resp = client
            .http_client
            .get(sign_url)
            .headers(header)
            .send()
            .await?;

        let res = parse_xml_response(resp).await?;
        Ok(res)
    }
}
// endregion: --- list objects v2

// region:    --- get bucket info
#[derive(Builder)]
pub struct GetBucketInfo<'a> {
    #[builder(start_fn)]
    pub(crate) client: &'a Client,
    pub(crate) bucket: &'a str,
}

#[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 versioning: Option<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,
    pub block_public_access: bool,
}

#[derive(Deserialize, Debug)]
pub struct ServerSideEncryptionRule {
    #[serde(rename = "SSEAlgorithm")]
    pub sse_algorithm: String,
    pub kms_master_key_id: Option<String>,
    pub kms_data_encryption: Option<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,
}

impl GetBucketInfo<'_> {
    pub async fn send(&self) -> Result<BucketInfo, Error> {
        let client = self.client;
        let request_url = Url::parse_with_params(
            &format!("https://{}.{}", self.bucket, client.endpoint),
            [("bucketInfo", "")],
        )
        .unwrap();

        let creds = client.credentials_provider.load().await?;
        let mut req_header_map = HashMap::new();
        if let Some(token) = &creds.sts_security_token {
            req_header_map.insert("x-oss-security-token".to_string(), token.clone());
        }

        let header_map = get_request_header(
            &creds.access_key_id,
            &creds.access_key_secret,
            req_header_map,
            &request_url,
            HTTPVerb::Get,
            &client.region,
            Some(&client.bucket),
        );
        let resp = client
            .http_client
            .get(request_url)
            .headers(header_map)
            .send()
            .await?;

        let res = parse_xml_response(resp).await?;
        Ok(res)
    }
}
// endregion: --- get bucket info

//region get bucket location
#[derive(Builder)]
pub struct GetBucketLocation<'a> {
    #[builder(start_fn)]
    pub(crate) client: &'a Client,
    pub(crate) bucket: &'a str,
}
// xml数据为:"<LocationConstraint>oss-cn-hangzhou</LocationConstraint>",
// 这种情况下使用xml反序列化比较特殊,写法得类似于下面这样:
#[derive(Deserialize)]
struct LocationConstraint {
    #[serde(rename = "$text")]
    field: String,
}

impl GetBucketLocation<'_> {
    pub async fn send(&self) -> Result<String, Error> {
        let client = self.client;

        let request_url = Url::parse_with_params(
            &format!("https://{}.{}", self.bucket, client.endpoint),
            [("location", "")],
        )
        .unwrap();

        let creds = client.credentials_provider.load().await?;
        let mut req_header_map = HashMap::new();
        if let Some(token) = &creds.sts_security_token {
            req_header_map.insert("x-oss-security-token".to_string(), token.clone());
        }

        let header_map = get_request_header(
            &creds.access_key_id,
            &creds.access_key_secret,
            req_header_map,
            &request_url,
            HTTPVerb::Get,
            &client.region,
            Some(&client.bucket),
        );
        let resp = client
            .http_client
            .get(request_url)
            .headers(header_map)
            .send()
            .await?;

        let res = parse_xml_response::<LocationConstraint>(resp).await?;
        Ok(res.field)
    }
}
//endregion

// region:    --- get bucket stat
#[derive(Builder)]
pub struct GetBucketStat<'a> {
    #[builder(start_fn)]
    pub(crate) client: &'a Client,
    pub(crate) bucket: &'a str,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct BucketStat {
    pub storage: u64,
    pub object_count: u32,
    pub multipart_upload_count: u32,
    pub delete_marker_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,
    pub deep_cold_archive_storage: u64,
    pub deep_cold_archive_real_storage: u64,
    pub deep_cold_archive_object_count: u32,
}

impl GetBucketStat<'_> {
    pub async fn send(&self) -> Result<BucketStat, Error> {
        let client = self.client;
        let request_url = Url::parse_with_params(
            &format!("https://{}.{}", self.bucket, client.endpoint),
            [("stat", "")],
        )
        .unwrap();

        let creds = client.credentials_provider.load().await?;
        let mut req_header_map = HashMap::new();
        if let Some(token) = &creds.sts_security_token {
            req_header_map.insert("x-oss-security-token".to_string(), token.clone());
        }

        let header_map = get_request_header(
            &creds.access_key_id,
            &creds.access_key_secret,
            req_header_map,
            &request_url,
            HTTPVerb::Get,
            &client.region,
            Some(&client.bucket),
        );

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

        let res = parse_xml_response(resp).await?;
        Ok(res)
    }
}
// endregion: --- get bucket stat

impl Client {
    pub fn put_bucket(&self) -> PutBucketBuilder<'_> {
        PutBucket::builder(self)
    }

    pub fn list_objects_v2(&self) -> ListObjectsV2Builder<'_> {
        ListObjectsV2::builder(self)
    }

    pub fn get_bucket_info(&self) -> GetBucketInfoBuilder<'_> {
        GetBucketInfo::builder(self)
    }

    pub fn get_bucket_location(&self) -> GetBucketLocationBuilder<'_> {
        GetBucketLocation::builder(self)
    }

    pub fn get_bucket_stat(&self) -> GetBucketStatBuilder<'_> {
        GetBucketStat::builder(self)
    }
}