Skip to content

Commit

Permalink
feat: add delete object method
Browse files Browse the repository at this point in the history
  • Loading branch information
tu6ge committed May 30, 2024
1 parent fc8f3cb commit 6a07f37
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum OssError {

Upload(String),

Delete(String),

NoFoundBucket,

ParseXml(#[from] serde_xml_rs::Error),
Expand Down
28 changes: 28 additions & 0 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 6a07f37

Please sign in to comment.