1use std::fmt::Debug;
19use std::sync::Arc;
20
21use http::StatusCode;
22use log::debug;
23use reqsign_azure_storage::DefaultCredentialProvider;
24use reqsign_azure_storage::RequestSigner;
25use reqsign_azure_storage::StaticCredentialProvider;
26use reqsign_core::Context;
27use reqsign_core::Env as _;
28use reqsign_core::OsEnv;
29use reqsign_core::Signer;
30use reqsign_core::StaticEnv;
31use reqsign_file_read_tokio::TokioFileRead;
32
33use super::AZFILE_SCHEME;
34use super::config::AzfileConfig;
35use super::core::X_MS_META_PREFIX;
36use super::core::parse_error;
37use super::core::{AzfileCore, ErrorContext};
38use super::deleter::AzfileDeleter;
39use super::lister::AzfileLister;
40use super::reader::*;
41use super::writer::AzfileWriter;
42use super::writer::AzfileWriters;
43use opendal_core::raw::*;
44use opendal_core::*;
45use opendal_service_azure_common::{
46 AzureStorageConfig as AzureConnectionConfig, AzureStorageService,
47 azure_account_name_from_endpoint, azure_config_from_connection_string,
48};
49
50impl From<AzureConnectionConfig> for AzfileConfig {
51 fn from(config: AzureConnectionConfig) -> Self {
52 AzfileConfig {
53 account_name: config.account_name,
54 account_key: config.account_key,
55 sas_token: config.sas_token,
56 endpoint: config.endpoint,
57 root: None, share_name: String::new(), }
60 }
61}
62
63#[doc = include_str!("docs.md")]
65#[derive(Default)]
66pub struct AzfileBuilder {
67 pub(super) config: AzfileConfig,
68}
69
70impl Debug for AzfileBuilder {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 f.debug_struct("AzfileBuilder")
73 .field("config", &self.config)
74 .finish_non_exhaustive()
75 }
76}
77
78impl AzfileBuilder {
79 pub fn root(mut self, root: &str) -> Self {
83 self.config.root = if root.is_empty() {
84 None
85 } else {
86 Some(root.to_string())
87 };
88
89 self
90 }
91
92 pub fn endpoint(mut self, endpoint: &str) -> Self {
94 if !endpoint.is_empty() {
95 self.config.endpoint = Some(endpoint.trim_end_matches('/').to_string());
97 }
98
99 self
100 }
101
102 pub fn account_name(mut self, account_name: &str) -> Self {
107 if !account_name.is_empty() {
108 self.config.account_name = Some(account_name.to_string());
109 }
110
111 self
112 }
113
114 pub fn account_key(mut self, account_key: &str) -> Self {
119 if !account_key.is_empty() {
120 self.config.account_key = Some(account_key.to_string());
121 }
122
123 self
124 }
125
126 pub fn share_name(mut self, share_name: &str) -> Self {
131 if !share_name.is_empty() {
132 self.config.share_name = share_name.to_string();
133 }
134
135 self
136 }
137
138 pub fn from_connection_string(conn_str: &str) -> Result<Self> {
157 let config = azure_config_from_connection_string(conn_str, AzureStorageService::File)?;
158
159 Ok(AzfileConfig::from(config).into_builder())
160 }
161}
162
163impl Builder for AzfileBuilder {
164 type Config = AzfileConfig;
165
166 fn build(self) -> Result<impl Service> {
167 debug!("backend build started: {:?}", self);
168
169 let root = normalize_root(&self.config.root.unwrap_or_default());
170 debug!("backend use root {root}");
171
172 let endpoint = match &self.config.endpoint {
173 Some(endpoint) => Ok(endpoint.clone()),
174 None => Err(Error::new(ErrorKind::ConfigInvalid, "endpoint is empty")
175 .with_operation("Builder::build")
176 .with_context("service", AZFILE_SCHEME)),
177 }?;
178 debug!("backend use endpoint {}", endpoint);
179
180 let account_name_option = self
181 .config
182 .account_name
183 .clone()
184 .or_else(|| azure_account_name_from_endpoint(endpoint.as_str()));
185
186 let account_name = match account_name_option {
187 Some(account_name) => Ok(account_name),
188 None => Err(
189 Error::new(ErrorKind::ConfigInvalid, "account_name is empty")
190 .with_operation("Builder::build")
191 .with_context("service", AZFILE_SCHEME),
192 ),
193 }?;
194
195 let mut envs = std::collections::HashMap::new();
196 envs.insert("AZBLOB_ACCOUNT_NAME".to_string(), account_name.clone());
197 envs.insert(
198 "AZURE_STORAGE_ACCOUNT_NAME".to_string(),
199 account_name.clone(),
200 );
201
202 if let Some(v) = &self.config.account_key {
203 envs.insert("AZBLOB_ACCOUNT_KEY".to_string(), v.clone());
204 envs.insert("AZURE_STORAGE_ACCOUNT_KEY".to_string(), v.clone());
205 }
206 if let Some(v) = &self.config.sas_token {
207 envs.insert("AZURE_STORAGE_SAS_TOKEN".to_string(), v.clone());
208 }
209
210 let os_env = OsEnv;
211 let ctx = Context::new()
212 .with_file_read(TokioFileRead)
213 .with_env(StaticEnv {
214 home_dir: os_env.home_dir(),
215 envs,
216 });
217
218 let mut credential = DefaultCredentialProvider::new();
219 if let Some(account_key) = self.config.account_key.as_deref() {
220 credential = credential.push_front(StaticCredentialProvider::new_shared_key(
221 &account_name,
222 account_key,
223 ));
224 }
225 if let Some(sas_token) = self.config.sas_token.as_deref() {
226 credential = credential.push_front(StaticCredentialProvider::new_sas_token(sas_token));
227 }
228
229 let sign_ctx = ctx;
230 let signer = Signer::new(sign_ctx.clone(), credential, RequestSigner::new());
231
232 let info = ServiceInfo::new(AZFILE_SCHEME, &root, "");
233 let capability = Capability {
234 stat: true,
235
236 read: true,
237
238 write: true,
239 write_with_user_metadata: true,
240 write_total_max_size: Some(4 * 1024 * 1024),
243
244 create_dir: true,
245 delete: true,
246 rename: true,
247
248 list: true,
249
250 shared: true,
251
252 ..Default::default()
253 };
254
255 Ok(AzfileBackend {
256 core: Arc::new(AzfileCore {
257 info,
258 capability,
259 root,
260 endpoint,
261 signer,
262 sign_ctx,
263 share_name: self.config.share_name.clone(),
264 }),
265 })
266 }
267}
268
269#[derive(Debug, Clone)]
271pub struct AzfileBackend {
272 pub(crate) core: Arc<AzfileCore>,
273}
274
275impl Service for AzfileBackend {
276 type Reader = oio::StreamReader<AzfileReader>;
277 type Writer = AzfileWriters;
278 type Lister = oio::PageLister<AzfileLister>;
279 type Deleter = oio::OneShotDeleter<AzfileDeleter>;
280 type Copier = ();
281 type Composer = ();
282
283 fn info(&self) -> ServiceInfo {
284 self.core.info.clone()
285 }
286
287 fn capability(&self) -> Capability {
288 self.core.capability
289 }
290
291 async fn create_dir(
292 &self,
293 ctx: &OperationContext,
294 path: &str,
295 _: OpCreateDir,
296 ) -> Result<RpCreateDir> {
297 self.core.ensure_parent_dir_exists(ctx, path).await?;
298 let resp = self.core.azfile_create_dir(ctx, path).await?;
299 let status = resp.status();
300
301 match status {
302 StatusCode::CREATED => Ok(RpCreateDir::default()),
303 _ => {
304 if resp
310 .headers()
311 .get("x-ms-error-code")
312 .map(|value| value.to_str().unwrap_or(""))
313 .unwrap_or_else(|| "")
314 == "ResourceAlreadyExists"
315 {
316 Ok(RpCreateDir::default())
317 } else {
318 Err(parse_error(
319 ErrorContext::new(ServiceOperation("CreateDirectory")),
320 resp,
321 ))
322 }
323 }
324 }
325 }
326
327 async fn stat(&self, ctx: &OperationContext, path: &str, _: OpStat) -> Result<RpStat> {
328 let resp = if path.ends_with('/') {
329 self.core.azfile_get_directory_properties(ctx, path).await?
330 } else {
331 self.core.azfile_get_file_properties(ctx, path).await?
332 };
333
334 let status = resp.status();
335 match status {
336 StatusCode::OK => {
337 let headers = resp.headers();
338 let mut meta = parse_into_metadata(path, headers)?.into_builder();
339 let user_meta = parse_prefixed_headers(headers, X_MS_META_PREFIX);
340 if !user_meta.is_empty() {
341 meta.user_metadata(user_meta);
342 }
343 Ok(RpStat::new(meta.build()))
344 }
345 _ => Err(parse_error(
346 ErrorContext::new(if path.ends_with('/') {
347 ServiceOperation("GetDirectoryProperties")
348 } else {
349 ServiceOperation("GetFileProperties")
350 }),
351 resp,
352 )),
353 }
354 }
355 fn read(&self, ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
356 let output: oio::StreamReader<AzfileReader> = {
357 Ok(oio::StreamReader::new(AzfileReader::new(
358 self.clone(),
359 ctx.clone(),
360 path,
361 args,
362 )))
363 }?;
364
365 Ok(output)
366 }
367
368 fn write(&self, ctx: &OperationContext, path: &str, args: OpWrite) -> Result<Self::Writer> {
369 let output: AzfileWriters = {
370 let w = AzfileWriter::new(
371 self.core.clone(),
372 ctx.clone(),
373 args.clone(),
374 path.to_string(),
375 );
376 let w = if args.append() {
377 AzfileWriters::Two(oio::AppendWriter::new(w))
378 } else {
379 AzfileWriters::One(oio::OneShotWriter::new(w))
380 };
381 Ok(w)
382 }?;
383
384 Ok(output)
385 }
386
387 fn delete(&self, ctx: &OperationContext) -> Result<Self::Deleter> {
388 let output: oio::OneShotDeleter<AzfileDeleter> = {
389 Ok(oio::OneShotDeleter::new(AzfileDeleter::new(
390 self.core.clone(),
391 ctx.clone(),
392 )))
393 }?;
394
395 Ok(output)
396 }
397
398 fn list(&self, ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
399 let output: oio::PageLister<AzfileLister> = {
400 let l = AzfileLister::new(
401 self.core.clone(),
402 ctx.clone(),
403 path.to_string(),
404 args.limit(),
405 );
406
407 Ok(oio::PageLister::new(l))
408 }?;
409
410 Ok(output)
411 }
412
413 fn copy(
414 &self,
415 _ctx: &OperationContext,
416 _from: &str,
417 _to: &str,
418 _args: OpCopy,
419 ) -> Result<Self::Copier> {
420 Err(Error::new(
421 ErrorKind::Unsupported,
422 "operation is not supported",
423 ))
424 }
425
426 async fn rename(
427 &self,
428 ctx: &OperationContext,
429 from: &str,
430 to: &str,
431 _: OpRename,
432 ) -> Result<RpRename> {
433 self.core.ensure_parent_dir_exists(ctx, to).await?;
434 let resp = self.core.azfile_rename(ctx, from, to).await?;
435 let status = resp.status();
436 match status {
437 StatusCode::OK => Ok(RpRename::default()),
438 _ => Err(parse_error(
439 ErrorContext::new(ServiceOperation("Rename")),
440 resp,
441 )),
442 }
443 }
444
445 async fn presign(
446 &self,
447 _ctx: &OperationContext,
448 _path: &str,
449 _args: OpPresign,
450 ) -> Result<RpPresign> {
451 Err(Error::new(
452 ErrorKind::Unsupported,
453 "operation is not supported",
454 ))
455 }
456}