Skip to content

Commit ee1c2eb

Browse files
authored
Add 'delete file'. (#512)
1 parent bb3dfa5 commit ee1c2eb

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

sdk/storage/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ required-features=["blob"]
134134
name="data_lake_00_file_system"
135135
required-features=["data_lake"]
136136
[[example]]
137-
name="data_lake_01_file_create"
137+
name="data_lake_01_file_create_delete"
138138
required-features=["data_lake"]
139139
[[example]]
140140
name="data_lake_02_file_upload"

sdk/storage/examples/data_lake_01_file_create.rs renamed to sdk/storage/examples/data_lake_01_file_create_delete.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
4242
.await?;
4343
println!("create file response == {:?}\n", create_file_response);
4444

45+
println!("deleting file '{}'...", file_path);
46+
let delete_file_response = file_system_client
47+
.delete_file(Context::default(), file_path, FileDeleteOptions::default())
48+
.await?;
49+
println!("delete_file file response == {:?}\n", delete_file_response);
50+
4551
println!("deleting file system...");
4652
let delete_fs_response = file_system_client.delete().execute().await?;
4753
println!("delete file system response == {:?}\n", delete_fs_response);

sdk/storage/src/data_lake/clients/file_system_client.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ impl FileSystemClient {
8888
Ok(FileCreateResponse::try_from(response).await?)
8989
}
9090

91+
pub async fn delete_file(
92+
&self,
93+
ctx: Context,
94+
file_path: &str,
95+
options: FileDeleteOptions<'_>,
96+
) -> Result<FileDeleteResponse, crate::Error> {
97+
let mut request = self.prepare_file_delete_request(file_path);
98+
let contents = DataLakeContext {};
99+
let mut pipeline_context = PipelineContext::new(ctx, contents);
100+
101+
options.decorate_request(&mut request)?;
102+
let response = self
103+
.pipeline()
104+
.send(&mut pipeline_context, &mut request)
105+
.await?;
106+
107+
Ok(FileDeleteResponse::try_from(response).await?)
108+
}
109+
91110
pub async fn rename_file(
92111
&self,
93112
ctx: Context,
@@ -201,6 +220,14 @@ impl FileSystemClient {
201220
.into()
202221
}
203222

223+
pub(crate) fn prepare_file_delete_request(&self, file_path: &str) -> azure_core::Request {
224+
let uri = format!("{}/{}", self.url(), file_path);
225+
http::request::Request::delete(uri)
226+
.body(bytes::Bytes::new()) // Request builder requires a body here
227+
.unwrap()
228+
.into()
229+
}
230+
204231
pub(crate) fn prepare_file_rename_request(
205232
&self,
206233
destination_file_path: &str,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use azure_core::headers::CommonStorageResponseHeaders;
2+
use azure_core::prelude::ContentLength;
3+
use azure_core::prelude::IfMatchCondition;
4+
use std::convert::TryInto;
5+
6+
use azure_core::{Request as HttpRequest, Response as HttpResponse};
7+
8+
#[derive(Debug, Clone, Default)]
9+
pub struct FileDeleteOptions<'a> {
10+
if_match_condition: Option<IfMatchCondition<'a>>,
11+
}
12+
13+
impl<'a> FileDeleteOptions<'a> {
14+
pub fn new() -> Self {
15+
Self {
16+
if_match_condition: None,
17+
}
18+
}
19+
20+
setters! {
21+
if_match_condition: IfMatchCondition<'a> => Some(if_match_condition),
22+
}
23+
24+
pub(crate) fn decorate_request(&self, req: &mut HttpRequest) -> Result<(), crate::Error> {
25+
azure_core::headers::add_optional_header2(&self.if_match_condition, req)?;
26+
azure_core::headers::add_mandatory_header2(&ContentLength::new(0), req)?; // Length is required for creating files
27+
28+
Ok(())
29+
}
30+
}
31+
32+
#[derive(Debug, Clone)]
33+
pub struct FileDeleteResponse {
34+
pub common_storage_response_headers: CommonStorageResponseHeaders,
35+
}
36+
37+
impl FileDeleteResponse {
38+
pub async fn try_from(response: HttpResponse) -> Result<Self, crate::Error> {
39+
let (_status_code, headers, _pinned_stream) = response.deconstruct();
40+
41+
let common_storage_response_headers = (&headers).try_into()?;
42+
43+
Ok(Self {
44+
common_storage_response_headers,
45+
})
46+
}
47+
}

sdk/storage/src/data_lake/operations/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
mod file_append;
66
mod file_create;
7+
mod file_delete;
78
mod file_flush;
89
mod file_rename;
910

1011
pub use file_append::*;
1112
pub use file_create::*;
13+
pub use file_delete::*;
1214
pub use file_flush::*;
1315
pub use file_rename::*;

sdk/storage/tests/data_lake.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async fn test_data_lake_file_system_functions() -> Result<(), Box<dyn Error + Se
9191
}
9292

9393
#[tokio::test]
94-
async fn test_data_lake_file_create_functions() -> Result<(), Box<dyn Error + Send + Sync>> {
94+
async fn test_data_lake_file_create_delete_functions() -> Result<(), Box<dyn Error + Send + Sync>> {
9595
let data_lake_client = create_data_lake_client().await.unwrap();
9696

9797
let file_system_name = format!(
@@ -123,6 +123,10 @@ async fn test_data_lake_file_create_functions() -> Result<(), Box<dyn Error + Se
123123
.create_file(Context::default(), file_path, FileCreateOptions::default())
124124
.await?;
125125

126+
file_system_client
127+
.delete_file(Context::default(), file_path, FileDeleteOptions::default())
128+
.await?;
129+
126130
file_system_client.delete().execute().await?;
127131

128132
Ok(())

0 commit comments

Comments
 (0)