|
| 1 | +//! Hugging Face cloud storage support via OpenDAL. |
| 2 | +//! |
| 3 | +//! Provides an [`ObjectStore`] implementation for `hf://` URLs by bridging |
| 4 | +//! OpenDAL's HF backend through `object_store_opendal`. |
| 5 | +//! |
| 6 | +//! Gated behind `#[cfg(feature = "hf")]`. |
| 7 | +
|
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use object_store::ObjectStore; |
| 11 | +use polars_error::{PolarsResult, polars_bail, polars_err, to_compute_err}; |
| 12 | +use polars_utils::pl_path::PlRefPath; |
| 13 | + |
| 14 | +use super::options::CloudOptions; |
| 15 | + |
| 16 | +/// Parse an `hf://` URL and build an [`ObjectStore`] backed by OpenDAL. |
| 17 | +/// |
| 18 | +/// Supported URL formats: |
| 19 | +/// - `hf://buckets/<namespace>/<name>[/<path>]` |
| 20 | +/// - `hf://datasets/<namespace>/<name>[/<path>]` |
| 21 | +/// - `hf://models/<namespace>/<name>[/<path>]` |
| 22 | +pub fn build_hf( |
| 23 | + url: PlRefPath, |
| 24 | + options: Option<&CloudOptions>, |
| 25 | +) -> PolarsResult<Arc<dyn ObjectStore>> { |
| 26 | + let after_scheme = url.strip_scheme(); |
| 27 | + let (repo_type_plural, rest) = after_scheme |
| 28 | + .split_once('/') |
| 29 | + .ok_or_else(|| polars_err!(ComputeError: "invalid hf:// URL: {}", url.as_str()))?; |
| 30 | + |
| 31 | + // hf:// URLs use plural form ("buckets", "datasets", "models") |
| 32 | + // but OpenDAL expects singular ("bucket", "dataset", "model") |
| 33 | + let repo_type: &str = repo_type_plural |
| 34 | + .strip_suffix('s') |
| 35 | + .unwrap_or(repo_type_plural); |
| 36 | + |
| 37 | + // Extract repo_id (namespace/name) from the remaining path |
| 38 | + let parts = rest.splitn(3, '/').collect::<Vec<&str>>(); |
| 39 | + if parts.len() < 2 || parts[0].is_empty() || parts[1].is_empty() { |
| 40 | + polars_bail!( |
| 41 | + ComputeError: |
| 42 | + "invalid hf:// URL: expected hf://<type>/<namespace>/<name>[/path], got: {}", |
| 43 | + url.as_str() |
| 44 | + ); |
| 45 | + } |
| 46 | + let repo_id = format!("{}/{}", parts[0], parts[1]); |
| 47 | + |
| 48 | + let token = extract_hf_token(options)?; |
| 49 | + |
| 50 | + let builder = opendal::services::Hf::default() |
| 51 | + .repo_type(repo_type) |
| 52 | + .repo_id(&repo_id) |
| 53 | + .token(&token); |
| 54 | + |
| 55 | + let op = opendal::Operator::new(builder) |
| 56 | + .map_err(to_compute_err)? |
| 57 | + .finish(); |
| 58 | + |
| 59 | + Ok(Arc::new(object_store_opendal::OpendalStore::new(op)) as Arc<dyn ObjectStore>) |
| 60 | +} |
| 61 | + |
| 62 | +/// Extract an HF token from cloud options, environment, or cached file. |
| 63 | +/// |
| 64 | +/// Resolution order: |
| 65 | +/// 1. `storage_options` / CloudOptions HTTP Authorization header |
| 66 | +/// 2. `HF_TOKEN` environment variable |
| 67 | +/// 3. Cached token at `$HF_HOME/token` (default: `~/.cache/huggingface/token`) |
| 68 | +fn extract_hf_token(cloud_options: Option<&CloudOptions>) -> PolarsResult<String> { |
| 69 | + #[cfg(feature = "http")] |
| 70 | + if let Some(opts) = cloud_options { |
| 71 | + if let Some(super::options::CloudConfig::Http { headers }) = &opts.config { |
| 72 | + for (key, value) in headers { |
| 73 | + if key.eq_ignore_ascii_case("authorization") { |
| 74 | + if let Some(token) = value.strip_prefix("Bearer ") { |
| 75 | + return Ok(token.to_string()); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + #[cfg(not(feature = "http"))] |
| 83 | + let _ = cloud_options; |
| 84 | + |
| 85 | + if let Ok(token) = std::env::var("HF_TOKEN") { |
| 86 | + if !token.is_empty() { |
| 87 | + return Ok(token); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + let hf_home = std::env::var("HF_HOME"); |
| 92 | + let hf_home = hf_home.as_deref().unwrap_or("~/.cache/huggingface"); |
| 93 | + let hf_home = crate::path_utils::resolve_homedir(hf_home); |
| 94 | + let cached_token_path = hf_home.join("token"); |
| 95 | + |
| 96 | + if let Ok(bytes) = std::fs::read(&cached_token_path) { |
| 97 | + if let Ok(token) = String::from_utf8(bytes) { |
| 98 | + let token = token.trim().to_string(); |
| 99 | + if !token.is_empty() { |
| 100 | + return Ok(token); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + polars_bail!( |
| 106 | + ComputeError: |
| 107 | + "no HF token found: set HF_TOKEN env var, pass via storage_options, \ |
| 108 | + or login with `huggingface-cli login`" |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +mod tests { |
| 114 | + use super::*; |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_token_from_env() { |
| 118 | + let original = std::env::var("HF_TOKEN").ok(); |
| 119 | + std::env::set_var("HF_TOKEN", "hf_test_token_123"); |
| 120 | + |
| 121 | + let result = extract_hf_token(None); |
| 122 | + assert!(result.is_ok()); |
| 123 | + assert_eq!(result.unwrap(), "hf_test_token_123"); |
| 124 | + |
| 125 | + match original { |
| 126 | + Some(v) => std::env::set_var("HF_TOKEN", v), |
| 127 | + None => std::env::remove_var("HF_TOKEN"), |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn test_empty_token_skipped() { |
| 133 | + let original = std::env::var("HF_TOKEN").ok(); |
| 134 | + std::env::set_var("HF_TOKEN", ""); |
| 135 | + |
| 136 | + let result = extract_hf_token(None); |
| 137 | + if let Ok(token) = &result { |
| 138 | + assert!(!token.is_empty()); |
| 139 | + } |
| 140 | + |
| 141 | + match original { |
| 142 | + Some(v) => std::env::set_var("HF_TOKEN", v), |
| 143 | + None => std::env::remove_var("HF_TOKEN"), |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn test_build_hf_valid_bucket_url() { |
| 149 | + std::env::set_var("HF_TOKEN", "hf_test"); |
| 150 | + let url = PlRefPath::new("hf://buckets/myorg/mybucket/path/file.parquet"); |
| 151 | + let result = build_hf(url, None); |
| 152 | + // Builder succeeds (actual I/O would fail without a real token, |
| 153 | + // but the ObjectStore is constructed) |
| 154 | + assert!(result.is_ok()); |
| 155 | + std::env::remove_var("HF_TOKEN"); |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn test_build_hf_valid_dataset_url() { |
| 160 | + std::env::set_var("HF_TOKEN", "hf_test"); |
| 161 | + let url = PlRefPath::new("hf://datasets/user/dataset-name/train.parquet"); |
| 162 | + let result = build_hf(url, None); |
| 163 | + assert!(result.is_ok()); |
| 164 | + std::env::remove_var("HF_TOKEN"); |
| 165 | + } |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn test_build_hf_invalid_url_no_repo() { |
| 169 | + std::env::set_var("HF_TOKEN", "hf_test"); |
| 170 | + let url = PlRefPath::new("hf://buckets/only-namespace"); |
| 171 | + let result = build_hf(url, None); |
| 172 | + assert!(result.is_err()); |
| 173 | + std::env::remove_var("HF_TOKEN"); |
| 174 | + } |
| 175 | +} |
0 commit comments