2929
3030class GoogleCloudStorageFileSourceTemplateConfiguration (FsspecBaseFileSourceTemplateConfiguration ):
3131 bucket_name : Union [str , TemplateExpansion ]
32+ root_path : Union [str , TemplateExpansion , None ] = None
3233 project : Union [str , TemplateExpansion , None ] = None
3334 anonymous : Union [bool , TemplateExpansion , None ] = True
3435 service_account_json : Union [str , TemplateExpansion , None ] = None
@@ -42,6 +43,7 @@ class GoogleCloudStorageFileSourceTemplateConfiguration(FsspecBaseFileSourceTemp
4243
4344class GoogleCloudStorageFileSourceConfiguration (FsspecBaseFileSourceConfiguration ):
4445 bucket_name : str
46+ root_path : Optional [str ] = None
4547 project : Optional [str ] = None
4648 anonymous : Optional [bool ] = True
4749 service_account_json : Optional [str ] = None
@@ -101,19 +103,29 @@ def _open_fs(
101103 return fs
102104
103105 def _to_bucket_path (self , path : str , config : GoogleCloudStorageFileSourceConfiguration ) -> str :
104- """Adapt the path to the GCS bucket format."""
106+ """Adapt the path to the GCS bucket format, including root_path if configured ."""
105107 bucket = config .bucket_name
108+ root = (config .root_path or "" ).strip ("/" )
106109 if path .startswith ("/" ):
107110 path = path [1 :]
108- return f"{ bucket } /{ path } " if path else bucket
111+ # Build path: bucket / root_path / path
112+ if root and path :
113+ return f"{ bucket } /{ root } /{ path } "
114+ elif root :
115+ return f"{ bucket } /{ root } "
116+ elif path :
117+ return f"{ bucket } /{ path } "
118+ return bucket
109119
110120 def _adapt_entry_path (self , filesystem_path : str ) -> str :
111- """Remove the GCS bucket name from the filesystem path."""
121+ """Remove the GCS bucket name and root_path from the filesystem path."""
112122 if self .template_config .bucket_name :
113- if filesystem_path == self .template_config .bucket_name :
123+ bucket = self .template_config .bucket_name
124+ root = (self .template_config .root_path or "" ).strip ("/" )
125+ full_prefix = f"{ bucket } /{ root } " if root else bucket
126+ if filesystem_path == full_prefix :
114127 return "/"
115- bucket_prefix = f"{ self .template_config .bucket_name } /"
116- return "/" + filesystem_path .removeprefix (bucket_prefix )
128+ return "/" + filesystem_path .removeprefix (f"{ full_prefix } /" )
117129 return "/" + filesystem_path
118130
119131 def _list (
0 commit comments