aliyun_oss_rs/bucket/
list_objects.rs1use crate::common::body_to_bytes;
2use crate::{
3 Error,
4 common::{Owner, StorageClass},
5 error::normal_error,
6 request::{Oss, OssRequest},
7};
8use http::Method;
9use serde_derive::Deserialize;
10use std::cmp;
11
12#[derive(Debug, Deserialize)]
14#[serde(rename_all = "PascalCase")]
15pub struct ObjectsList {
16 pub next_continuation_token: Option<String>,
18 pub contents: Option<Vec<ObjectInfo>>,
20 pub common_prefixes: Option<Vec<CommonPrefixes>>,
22}
23
24#[derive(Debug, Deserialize)]
26#[serde(rename_all = "PascalCase")]
27pub struct ObjectInfo {
28 pub key: String,
30 pub last_modified: String,
32 pub e_tag: String,
34 #[serde(rename = "Type")]
35 pub type_field: String,
36 pub size: u64,
38 pub storage_class: StorageClass,
40 pub restore_info: Option<String>,
42 pub owner: Option<Owner>,
44}
45
46#[derive(Debug, Deserialize)]
48#[serde(rename_all = "PascalCase")]
49pub struct CommonPrefixes {
50 pub prefix: String,
52}
53
54pub struct ListObjects {
60 req: OssRequest,
61}
62
63impl ListObjects {
64 pub(super) fn new(oss: Oss) -> Self {
65 let mut req = OssRequest::new(oss, Method::GET);
66 req.insert_query("list-type", "2");
67 req.insert_query("max-keys", "1000");
68 ListObjects { req }
69 }
70 pub fn set_delimiter(mut self, delimiter: impl ToString) -> Self {
72 self.req.insert_query("delimiter", delimiter);
73 self
74 }
75 pub fn set_start_after(mut self, start_after: impl ToString) -> Self {
81 self.req.insert_query("start-after", start_after);
82 self
83 }
84 pub fn set_continuation_token(mut self, continuation_token: impl ToString) -> Self {
88 self.req
89 .insert_query("continuation-token", continuation_token);
90 self
91 }
92 pub fn set_prefix(mut self, prefix: impl ToString) -> Self {
94 self.req.insert_query("prefix", prefix.to_string());
95 self
96 }
97 pub fn set_max_keys(mut self, max_keys: u32) -> Self {
103 let max_keys = cmp::min(1000, cmp::max(1, max_keys));
104 self.req.insert_query("max-keys", max_keys);
105 self
106 }
107 pub fn fetch_owner(mut self) -> Self {
109 self.req.insert_query("fetch-owner", "true");
110 self
111 }
112 pub async fn send(self) -> Result<ObjectsList, Error> {
115 let response = self.req.send_to_oss()?.await?;
117 let status_code = response.status();
119 match status_code {
120 code if code.is_success() => {
121 let response_bytes = body_to_bytes(response.into_body())
122 .await
123 .map_err(|_| Error::OssInvalidResponse(None))?;
124 let object_list: ObjectsList = serde_xml_rs::from_reader(&*response_bytes)
125 .map_err(|_| Error::OssInvalidResponse(Some(response_bytes)))?;
126 Ok(object_list)
127 }
128 _ => return Err(normal_error(response).await),
129 }
130 }
131}