Skip to main content

opendal_service_azfile/
backend.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use 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::AzfileCore;
36use super::core::X_MS_META_PREFIX;
37use super::core::parse_error;
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,                // root is not part of AzureStorageConfig
58            share_name: String::new(), // share_name is not part of AzureStorageConfig
59        }
60    }
61}
62
63/// Azure File services support.
64#[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    /// Set root of this backend.
80    ///
81    /// All operations will happen under this root.
82    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    /// Set endpoint of this backend.
93    pub fn endpoint(mut self, endpoint: &str) -> Self {
94        if !endpoint.is_empty() {
95            // Trim trailing `/` so that we can accept `http://127.0.0.1:9000/`
96            self.config.endpoint = Some(endpoint.trim_end_matches('/').to_string());
97        }
98
99        self
100    }
101
102    /// Set account_name of this backend.
103    ///
104    /// - If account_name is set, we will take user's input first.
105    /// - If not, we will try to load it from environment.
106    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    /// Set account_key of this backend.
115    ///
116    /// - If account_key is set, we will take user's input first.
117    /// - If not, we will try to load it from environment.
118    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    /// Set file share name of this backend.
127    ///
128    /// # Notes
129    /// You can find more about from: <https://learn.microsoft.com/en-us/rest/api/storageservices/operations-on-shares--file-service>
130    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    /// Create a new `AfileBuilder` instance from an [Azure Storage connection string][1].
139    ///
140    /// [1]: https://learn.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string
141    ///
142    /// # Example
143    /// ```
144    /// use opendal_core::Builder;
145    /// use opendal_service_azfile::Azfile;
146    ///
147    /// let conn_str = "AccountName=example;DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net";
148    ///
149    /// let mut config = Azfile::from_connection_string(&conn_str)
150    ///     .unwrap()
151    ///     // Add additional configuration if needed
152    ///     .share_name("myShare")
153    ///     .build()
154    ///     .unwrap();
155    /// ```
156    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
241            create_dir: true,
242            delete: true,
243            rename: true,
244
245            list: true,
246
247            shared: true,
248
249            ..Default::default()
250        };
251
252        Ok(AzfileBackend {
253            core: Arc::new(AzfileCore {
254                info,
255                capability,
256                root,
257                endpoint,
258                signer,
259                sign_ctx,
260                share_name: self.config.share_name.clone(),
261            }),
262        })
263    }
264}
265
266/// Backend for azfile services.
267#[derive(Debug, Clone)]
268pub struct AzfileBackend {
269    pub(crate) core: Arc<AzfileCore>,
270}
271
272impl Service for AzfileBackend {
273    type Reader = oio::StreamReader<AzfileReader>;
274    type Writer = AzfileWriters;
275    type Lister = oio::PageLister<AzfileLister>;
276    type Deleter = oio::OneShotDeleter<AzfileDeleter>;
277    type Copier = ();
278
279    fn info(&self) -> ServiceInfo {
280        self.core.info.clone()
281    }
282
283    fn capability(&self) -> Capability {
284        self.core.capability
285    }
286
287    async fn create_dir(
288        &self,
289        ctx: &OperationContext,
290        path: &str,
291        _: OpCreateDir,
292    ) -> Result<RpCreateDir> {
293        self.core.ensure_parent_dir_exists(ctx, path).await?;
294        let resp = self.core.azfile_create_dir(ctx, path).await?;
295        let status = resp.status();
296
297        match status {
298            StatusCode::CREATED => Ok(RpCreateDir::default()),
299            _ => {
300                // we cannot just check status code because 409 Conflict has two meaning:
301                // 1. If a directory by the same name is being deleted when Create Directory is called, the server returns status code 409 (Conflict)
302                // 2. If a directory or file with the same name already exists, the operation fails with status code 409 (Conflict).
303                // but we just need case 2 (already exists)
304                // ref: https://learn.microsoft.com/en-us/rest/api/storageservices/create-directory
305                if resp
306                    .headers()
307                    .get("x-ms-error-code")
308                    .map(|value| value.to_str().unwrap_or(""))
309                    .unwrap_or_else(|| "")
310                    == "ResourceAlreadyExists"
311                {
312                    Ok(RpCreateDir::default())
313                } else {
314                    Err(parse_error(resp))
315                }
316            }
317        }
318    }
319
320    async fn stat(&self, ctx: &OperationContext, path: &str, _: OpStat) -> Result<RpStat> {
321        let resp = if path.ends_with('/') {
322            self.core.azfile_get_directory_properties(ctx, path).await?
323        } else {
324            self.core.azfile_get_file_properties(ctx, path).await?
325        };
326
327        let status = resp.status();
328        match status {
329            StatusCode::OK => {
330                let headers = resp.headers();
331                let mut meta = parse_into_metadata(path, headers)?;
332                let user_meta = parse_prefixed_headers(headers, X_MS_META_PREFIX);
333                if !user_meta.is_empty() {
334                    meta = meta.with_user_metadata(user_meta);
335                }
336                Ok(RpStat::new(meta))
337            }
338            _ => Err(parse_error(resp)),
339        }
340    }
341    fn read(&self, ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
342        let output: oio::StreamReader<AzfileReader> = {
343            Ok(oio::StreamReader::new(AzfileReader::new(
344                self.clone(),
345                ctx.clone(),
346                path,
347                args,
348            )))
349        }?;
350
351        Ok(output)
352    }
353
354    fn write(&self, ctx: &OperationContext, path: &str, args: OpWrite) -> Result<Self::Writer> {
355        let output: AzfileWriters = {
356            let w = AzfileWriter::new(
357                self.core.clone(),
358                ctx.clone(),
359                args.clone(),
360                path.to_string(),
361            );
362            let w = if args.append() {
363                AzfileWriters::Two(oio::AppendWriter::new(w))
364            } else {
365                AzfileWriters::One(oio::OneShotWriter::new(w))
366            };
367            Ok(w)
368        }?;
369
370        Ok(output)
371    }
372
373    fn delete(&self, ctx: &OperationContext) -> Result<Self::Deleter> {
374        let output: oio::OneShotDeleter<AzfileDeleter> = {
375            Ok(oio::OneShotDeleter::new(AzfileDeleter::new(
376                self.core.clone(),
377                ctx.clone(),
378            )))
379        }?;
380
381        Ok(output)
382    }
383
384    fn list(&self, ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
385        let output: oio::PageLister<AzfileLister> = {
386            let l = AzfileLister::new(
387                self.core.clone(),
388                ctx.clone(),
389                path.to_string(),
390                args.limit(),
391            );
392
393            Ok(oio::PageLister::new(l))
394        }?;
395
396        Ok(output)
397    }
398
399    fn copy(
400        &self,
401        _ctx: &OperationContext,
402        _from: &str,
403        _to: &str,
404        _args: OpCopy,
405        _opts: OpCopier,
406    ) -> Result<Self::Copier> {
407        Err(Error::new(
408            ErrorKind::Unsupported,
409            "operation is not supported",
410        ))
411    }
412
413    async fn rename(
414        &self,
415        ctx: &OperationContext,
416        from: &str,
417        to: &str,
418        _: OpRename,
419    ) -> Result<RpRename> {
420        self.core.ensure_parent_dir_exists(ctx, to).await?;
421        let resp = self.core.azfile_rename(ctx, from, to).await?;
422        let status = resp.status();
423        match status {
424            StatusCode::OK => Ok(RpRename::default()),
425            _ => Err(parse_error(resp)),
426        }
427    }
428
429    async fn presign(
430        &self,
431        _ctx: &OperationContext,
432        _path: &str,
433        _args: OpPresign,
434    ) -> Result<RpPresign> {
435        Err(Error::new(
436            ErrorKind::Unsupported,
437            "operation is not supported",
438        ))
439    }
440}