diff --git a/src/error.rs b/src/error.rs index cd4d3d9..e7d6f48 100644 --- a/src/error.rs +++ b/src/error.rs @@ -32,6 +32,8 @@ pub enum OssError { Upload(String), + Delete(String), + NoFoundBucket, ParseXml(#[from] serde_xml_rs::Error), diff --git a/src/object.rs b/src/object.rs index 9e720b6..1782414 100644 --- a/src/object.rs +++ b/src/object.rs @@ -207,6 +207,28 @@ impl Object { Ok(response.into()) } + + pub async fn delete(&self, client: &Client) -> Result<(), OssError> { + let bucket = client.bucket().ok_or(OssError::NoFoundBucket)?; + let url = self.to_url(bucket); + let method = Method::DELETE; + let resource = CanonicalizedResource::new(format!("/{}/{}", bucket.as_str(), self.path)); + + let header_map = client.authorization(method, resource)?; + + let response = reqwest::Client::new() + .delete(url) + .headers(header_map) + .send() + .await?; + + if response.status().is_success() { + Ok(()) + } else { + let body = response.text().await?; + Err(OssError::Delete(body)) + } + } } #[derive(Debug)] @@ -279,6 +301,12 @@ mod tests { println!("{:?}", std::str::from_utf8(&info).unwrap()); } + #[tokio::test] + async fn test_delete() { + let object = Object::new("abc.txt"); + + let info = object.delete(&set_client()).await.unwrap(); + } #[tokio::test] async fn test_next_list() {