ossify 0.4.0

A modern, easy-to-use, and reqwest-powered Rust SDK for Alibaba Cloud Object Storage Service (OSS)
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
//! GetBucket (ListObjects V1): list all objects in a bucket using the legacy
//! marker-based pagination.
//!
//! New applications should prefer [`ListObjects`](super::ListObjects) (V2),
//! which supports continuation tokens. V1 is kept for compatibility with
//! existing integrations that rely on `Marker` / `NextMarker`.
//!
//! Official document: <https://www.alibabacloud.com/help/en/oss/developer-reference/listobjects>

use std::future::Future;

use http::Method;
use serde::{Deserialize, Deserializer, Serialize};
use serde_with::skip_serializing_none;

use crate::body::NoneBody;
use crate::error::Result;
use crate::ops::common::{EncodingType, ObjectType, Owner, StorageClass};
use crate::response::BodyResponseProcessor;
use crate::{Client, Ops, Prepared, QueryAuthOptions, Request};

/// Query parameters for [`ListObjectsV1`].
#[skip_serializing_none]
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct ListObjectsV1Params {
    /// Character used to group object names into `CommonPrefixes`.
    pub delimiter: Option<String>,
    /// Start listing from the object alphabetically after `marker`.
    pub marker: Option<String>,
    /// Maximum objects to return (1-1000, default 100).
    pub max_keys: Option<u32>,
    /// Return only objects whose key begins with this prefix.
    pub prefix: Option<String>,
    /// Encoding applied to the returned Delimiter, Marker, Prefix, NextMarker,
    /// and Key fields. Only `url` is supported.
    pub encoding_type: Option<EncodingType>,
}

impl ListObjectsV1Params {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn delimiter(mut self, delimiter: impl Into<String>) -> Self {
        self.delimiter = Some(delimiter.into());
        self
    }

    pub fn marker(mut self, marker: impl Into<String>) -> Self {
        self.marker = Some(marker.into());
        self
    }

    pub fn max_keys(mut self, max_keys: u32) -> Self {
        self.max_keys = Some(max_keys);
        self
    }

    pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
        self.prefix = Some(prefix.into());
        self
    }

    pub fn encoding_type(mut self, encoding_type: EncodingType) -> Self {
        self.encoding_type = Some(encoding_type);
        self
    }
}

/// Per-object metadata entry inside a [`ListObjectsV1Result`].
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ObjectSummaryV1 {
    /// Object key (name).
    pub key: String,
    /// Time the object was last modified, e.g. `2012-02-24T08:42:32.000Z`.
    pub last_modified: String,
    /// Entity tag - MD5 for simple uploads, non-MD5 for other methods.
    #[serde(rename = "ETag")]
    pub etag: String,
    /// Object type: `Normal`, `Multipart`, `Appendable`, or `Symlink`.
    #[serde(rename = "Type")]
    pub object_type: ObjectType,
    /// Size in bytes.
    pub size: u64,
    /// Storage class.
    pub storage_class: StorageClass,
    /// Set when the object has been transitioned to Cold Archive / Deep Cold
    /// Archive by a lifecycle rule.
    pub transition_time: Option<String>,
    /// Set when a sealed append object has been sealed.
    pub sealed_time: Option<String>,
    /// Restore status for Cold / Deep Cold Archive objects. Values look like
    /// `ongoing-request="true"` or
    /// `ongoing-request="false", expiry-date="Thu, 24 Sep 2020 12:40:33 GMT"`.
    pub restore_info: Option<String>,
    /// Owner information.
    pub owner: Option<Owner>,
}

fn deserialize_opt_u32<'de, D>(deserializer: D) -> std::result::Result<Option<u32>, D::Error>
where
    D: Deserializer<'de>,
{
    // OSS occasionally returns an empty <MaxKeys></MaxKeys> element (see the
    // archive-bucket example in the official docs). serde's default
    // `Option<u32>` deserialization would fail with "invalid type: string \"\"",
    // so we accept an optional string and parse it when non-empty.
    let s = Option::<String>::deserialize(deserializer)?;
    match s.as_deref() {
        None | Some("") => Ok(None),
        Some(v) => v.parse::<u32>().map(Some).map_err(serde::de::Error::custom),
    }
}

fn unwrap_common_prefixes<'de, D>(deserializer: D) -> std::result::Result<Vec<String>, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(rename_all = "PascalCase")]
    struct CommonPrefixes {
        #[serde(default)]
        prefix: Vec<String>,
    }

    let common_prefixes = Vec::<CommonPrefixes>::deserialize(deserializer)?;
    Ok(common_prefixes.into_iter().flat_map(|v| v.prefix).collect())
}

/// Response body for [`ListObjectsV1`].
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ListObjectsV1Result {
    /// Bucket name.
    pub name: String,
    /// Echo of the request `prefix`. Omitted (empty) when the request did not
    /// specify one.
    pub prefix: Option<String>,
    /// Echo of the request `marker`.
    pub marker: Option<String>,
    /// Echo of the request `max-keys`. Some OSS error responses (e.g. archive
    /// listing samples in the docs) emit an empty `<MaxKeys></MaxKeys>` element,
    /// so this is modelled as optional rather than `u32`.
    #[serde(default, deserialize_with = "deserialize_opt_u32")]
    pub max_keys: Option<u32>,
    /// Echo of the request `delimiter`.
    pub delimiter: Option<String>,
    /// Echo of the request `encoding-type`, if any.
    pub encoding_type: Option<EncodingType>,
    /// Whether the listing was truncated.
    pub is_truncated: bool,
    /// Marker to pass as `marker` in the next request when `is_truncated` is
    /// true. Note V1 uses `NextMarker`, not `NextContinuationToken`.
    pub next_marker: Option<String>,
    /// Individual objects.
    #[serde(default)]
    pub contents: Vec<ObjectSummaryV1>,
    /// Common prefixes when `delimiter` was specified.
    #[serde(default, deserialize_with = "unwrap_common_prefixes")]
    pub common_prefixes: Vec<String>,
}

/// The `ListObjects` V1 (GetBucket) operation.
pub struct ListObjectsV1 {
    pub query: ListObjectsV1Params,
}

impl ListObjectsV1 {
    pub fn new(params: Option<ListObjectsV1Params>) -> Self {
        Self {
            query: params.unwrap_or_default(),
        }
    }
}

impl Ops for ListObjectsV1 {
    type Response = BodyResponseProcessor<ListObjectsV1Result>;
    type Body = NoneBody;
    type Query = ListObjectsV1Params;

    fn prepare(self) -> Result<Prepared<ListObjectsV1Params>> {
        Ok(Prepared {
            method: Method::GET,
            query: Some(self.query),
            ..Default::default()
        })
    }
}

pub trait ListObjectsV1Ops {
    /// List objects in a bucket using the legacy V1 (GetBucket) protocol.
    ///
    /// New integrations should prefer [`ListObjectsOps::list_objects`](super::ListObjectsOps::list_objects)
    /// (the V2 API). V1 exists for compatibility.
    ///
    /// Official document: <https://www.alibabacloud.com/help/en/oss/developer-reference/listobjects>
    fn list_objects_v1(
        &self,
        params: Option<ListObjectsV1Params>,
    ) -> impl Future<Output = Result<ListObjectsV1Result>>;

    /// Presign a list-objects V1 URL.
    fn presign_list_objects_v1(
        &self,
        public: bool,
        params: Option<ListObjectsV1Params>,
        query_auth_options: QueryAuthOptions,
    ) -> impl Future<Output = Result<String>>;
}

impl ListObjectsV1Ops for Client {
    async fn list_objects_v1(&self, params: Option<ListObjectsV1Params>) -> Result<ListObjectsV1Result> {
        let ops = ListObjectsV1::new(params);
        self.request(ops).await
    }

    async fn presign_list_objects_v1(
        &self,
        public: bool,
        params: Option<ListObjectsV1Params>,
        query_auth_options: QueryAuthOptions,
    ) -> Result<String> {
        let ops = ListObjectsV1::new(params);
        self.presign(ops, public, Some(query_auth_options)).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn params_serialize_empty() {
        let q = crate::ser::to_string(&ListObjectsV1Params::new()).unwrap();
        assert_eq!(q, "");
    }

    #[test]
    fn params_serialize_all_fields() {
        let q = crate::ser::to_string(
            &ListObjectsV1Params::new()
                .delimiter("/")
                .marker("m")
                .max_keys(50)
                .prefix("fun/")
                .encoding_type(EncodingType::Url),
        )
        .unwrap();
        // Alphabetical order per ser::MapSerializer.
        assert_eq!(q, "delimiter=%2F&encoding-type=url&marker=m&max-keys=50&prefix=fun%2F");
    }

    #[test]
    fn prepared_uses_get_without_key() {
        let prepared = ListObjectsV1::new(None).prepare().unwrap();
        assert_eq!(prepared.method, Method::GET);
        assert!(prepared.key.is_none());
    }

    #[test]
    fn parse_simple_response() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult>
<Name>examplebucket</Name>
<Prefix></Prefix>
<Marker></Marker>
<MaxKeys>100</MaxKeys>
<Delimiter></Delimiter>
<IsTruncated>false</IsTruncated>
<Contents>
  <Key>fun/test.jpg</Key>
  <LastModified>2012-02-24T08:42:32.000Z</LastModified>
  <ETag>"5B3C1A2E053D763E1B002CC607C5A0FE1****"</ETag>
  <Type>Normal</Type>
  <Size>344606</Size>
  <StorageClass>Standard</StorageClass>
  <Owner>
    <ID>0022012****</ID>
    <DisplayName>user-example</DisplayName>
  </Owner>
</Contents>
</ListBucketResult>"#;
        let parsed: ListObjectsV1Result = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(parsed.name, "examplebucket");
        assert_eq!(parsed.max_keys, Some(100));
        assert!(!parsed.is_truncated);
        assert_eq!(parsed.contents.len(), 1);
        assert_eq!(parsed.contents[0].key, "fun/test.jpg");
        assert_eq!(parsed.contents[0].size, 344606);
        assert_eq!(parsed.contents[0].object_type, ObjectType::Normal);
        assert_eq!(parsed.contents[0].storage_class, StorageClass::Standard);
    }

    #[test]
    fn parse_response_with_common_prefixes() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult>
<Name>examplebucket</Name>
<Prefix>fun/</Prefix>
<Marker></Marker>
<MaxKeys>100</MaxKeys>
<Delimiter>/</Delimiter>
<IsTruncated>false</IsTruncated>
<Contents>
  <Key>fun/test.jpg</Key>
  <LastModified>2012-02-24T08:42:32.000Z</LastModified>
  <ETag>"abc"</ETag>
  <Type>Normal</Type>
  <Size>100</Size>
  <StorageClass>Standard</StorageClass>
</Contents>
<CommonPrefixes>
  <Prefix>fun/movie/</Prefix>
</CommonPrefixes>
</ListBucketResult>"#;
        let parsed: ListObjectsV1Result = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(parsed.common_prefixes, vec!["fun/movie/".to_string()]);
        assert_eq!(parsed.contents.len(), 1);
    }

    #[test]
    fn parse_paginated_response() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult>
<Name>examplebucket</Name>
<Prefix></Prefix>
<Marker>test1.txt</Marker>
<MaxKeys>2</MaxKeys>
<Delimiter></Delimiter>
<EncodingType>url</EncodingType>
<IsTruncated>true</IsTruncated>
<NextMarker>test100.txt</NextMarker>
<Contents>
  <Key>test10.txt</Key>
  <LastModified>2020-05-26T07:50:18.000Z</LastModified>
  <ETag>"abc"</ETag>
  <Type>Normal</Type>
  <Size>1</Size>
  <StorageClass>Standard</StorageClass>
</Contents>
</ListBucketResult>"#;
        let parsed: ListObjectsV1Result = quick_xml::de::from_str(xml).unwrap();
        assert!(parsed.is_truncated);
        assert_eq!(parsed.next_marker.as_deref(), Some("test100.txt"));
        assert_eq!(parsed.encoding_type, Some(EncodingType::Url));
    }

    #[test]
    fn parse_response_with_restore_info() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult>
<Name>examplebucket</Name>
<Prefix></Prefix>
<Marker></Marker>
<MaxKeys>100</MaxKeys>
<Delimiter></Delimiter>
<IsTruncated>false</IsTruncated>
<Contents>
  <Key>ex.txt</Key>
  <LastModified>2020-06-22T11:42:32.000Z</LastModified>
  <ETag>"e"</ETag>
  <Type>Normal</Type>
  <Size>1</Size>
  <StorageClass>Standard</StorageClass>
  <RestoreInfo>ongoing-request="false", expiry-date="Thu, 24 Sep 2020 12:40:33 GMT"</RestoreInfo>
</Contents>
</ListBucketResult>"#;
        let parsed: ListObjectsV1Result = quick_xml::de::from_str(xml).unwrap();
        assert!(
            parsed.contents[0]
                .restore_info
                .as_ref()
                .unwrap()
                .contains("ongoing-request")
        );
    }

    #[test]
    fn parse_response_with_transition_and_sealed_times() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult>
<Name>examplebucket</Name>
<MaxKeys>100</MaxKeys>
<IsTruncated>false</IsTruncated>
<Contents>
  <Key>movie/001.avi</Key>
  <TransitionTime>2024-04-23T07:21:42.000Z</TransitionTime>
  <LastModified>2012-02-24T08:43:07.000Z</LastModified>
  <ETag>"e"</ETag>
  <Type>Normal</Type>
  <Size>1</Size>
  <StorageClass>ColdArchive</StorageClass>
</Contents>
<Contents>
  <Key>sealed-append.log</Key>
  <LastModified>2020-05-21T12:07:15.000Z</LastModified>
  <SealedTime>2020-05-21T12:07:15.000Z</SealedTime>
  <ETag>"e"</ETag>
  <Type>Appendable</Type>
  <Size>1</Size>
  <StorageClass>Standard</StorageClass>
</Contents>
</ListBucketResult>"#;
        let parsed: ListObjectsV1Result = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(parsed.contents.len(), 2);
        assert_eq!(parsed.contents[0].transition_time.as_deref(), Some("2024-04-23T07:21:42.000Z"));
        assert_eq!(parsed.contents[0].storage_class, StorageClass::ColdArchive);
        assert_eq!(parsed.contents[1].sealed_time.as_deref(), Some("2020-05-21T12:07:15.000Z"));
        assert_eq!(parsed.contents[1].object_type, ObjectType::Appendable);
    }

    #[test]
    fn parse_response_with_empty_max_keys() {
        // Archive-bucket sample in the OSS docs emits <MaxKeys></MaxKeys>.
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult>
<Name>examplebucket</Name>
<Prefix></Prefix>
<Marker></Marker>
<MaxKeys></MaxKeys>
<Delimiter></Delimiter>
<IsTruncated>false</IsTruncated>
</ListBucketResult>"#;
        let parsed: ListObjectsV1Result = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(parsed.max_keys, None);
    }
}