lance_io/object_store/providers/
azure.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4use std::{collections::HashMap, str::FromStr, sync::Arc, time::Duration};
5
6use object_store::{
7    azure::{AzureConfigKey, MicrosoftAzureBuilder},
8    RetryConfig,
9};
10use url::Url;
11
12use crate::object_store::{
13    ObjectStore, ObjectStoreParams, ObjectStoreProvider, StorageOptions, DEFAULT_CLOUD_BLOCK_SIZE,
14    DEFAULT_CLOUD_IO_PARALLELISM, DEFAULT_MAX_IOP_SIZE,
15};
16use lance_core::error::Result;
17
18#[derive(Default, Debug)]
19pub struct AzureBlobStoreProvider;
20
21#[async_trait::async_trait]
22impl ObjectStoreProvider for AzureBlobStoreProvider {
23    async fn new_store(&self, base_path: Url, params: &ObjectStoreParams) -> Result<ObjectStore> {
24        let block_size = params.block_size.unwrap_or(DEFAULT_CLOUD_BLOCK_SIZE);
25        let mut storage_options =
26            StorageOptions(params.storage_options.clone().unwrap_or_default());
27        let download_retry_count = storage_options.download_retry_count();
28
29        let max_retries = storage_options.client_max_retries();
30        let retry_timeout = storage_options.client_retry_timeout();
31        let retry_config = RetryConfig {
32            backoff: Default::default(),
33            max_retries,
34            retry_timeout: Duration::from_secs(retry_timeout),
35        };
36
37        storage_options.with_env_azure();
38        let mut builder = MicrosoftAzureBuilder::new()
39            .with_url(base_path.as_ref())
40            .with_retry(retry_config);
41        for (key, value) in storage_options.as_azure_options() {
42            builder = builder.with_config(key, value);
43        }
44        let inner = Arc::new(builder.build()?);
45
46        Ok(ObjectStore {
47            inner,
48            scheme: String::from("az"),
49            block_size,
50            max_iop_size: *DEFAULT_MAX_IOP_SIZE,
51            use_constant_size_upload_parts: false,
52            list_is_lexically_ordered: true,
53            io_parallelism: DEFAULT_CLOUD_IO_PARALLELISM,
54            download_retry_count,
55        })
56    }
57}
58
59impl StorageOptions {
60    /// Add values from the environment to storage options
61    pub fn with_env_azure(&mut self) {
62        for (os_key, os_value) in std::env::vars_os() {
63            if let (Some(key), Some(value)) = (os_key.to_str(), os_value.to_str()) {
64                if let Ok(config_key) = AzureConfigKey::from_str(&key.to_ascii_lowercase()) {
65                    if !self.0.contains_key(config_key.as_ref()) {
66                        self.0
67                            .insert(config_key.as_ref().to_string(), value.to_string());
68                    }
69                }
70            }
71        }
72    }
73
74    /// Subset of options relevant for azure storage
75    pub fn as_azure_options(&self) -> HashMap<AzureConfigKey, String> {
76        self.0
77            .iter()
78            .filter_map(|(key, value)| {
79                let az_key = AzureConfigKey::from_str(&key.to_ascii_lowercase()).ok()?;
80                Some((az_key, value.clone()))
81            })
82            .collect()
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn test_azure_store_path() {
92        let provider = AzureBlobStoreProvider;
93
94        let url = Url::parse("az://bucket/path/to/file").unwrap();
95        let path = provider.extract_path(&url);
96        let expected_path = object_store::path::Path::from("path/to/file");
97        assert_eq!(path, expected_path);
98    }
99}