Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to load files with a colon in filename (Windows Client only) #439

Open
glusin opened this issue Jun 4, 2024 · 3 comments
Open

Unable to load files with a colon in filename (Windows Client only) #439

glusin opened this issue Jun 4, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@glusin
Copy link

glusin commented Jun 4, 2024

Filenames on cloud are allowed to contain colons, but it is not allowed in Windows filenames.
When a file is downloaded from a cloud it is first saved in a temp file. If filename contains colons then the code fails

@pjbull
Copy link
Member

pjbull commented Jun 6, 2024

Thanks for the issue @glusin.

What were you trying to do when you encountered the issue? Can you provide a short, reproducible example and the stack trace that accompanies the error?

@glusin
Copy link
Author

glusin commented Jun 6, 2024

We have a class to handle blob:// paths that inherits from AzureCloudPath.
For az:// the example would look like this:

from cloudpathlib import CloudPath, AzureBlobClient

client = AzureBlobClient(
    account_url="account_url",
    credential=credentials,
)

file_path = CloudPath('az://path/to/a/file:containing:colons', client=client)
file_path.open()  # crashes here

This is a traceback:


    File "C:\Users\PawelGlusinski\PycharmProjects\project\temp.py", line 5, in <module>
    file_path.open()
  File "C:\Users\PawelGlusinski\AppData\Local\pypoetry\Cache\virtualenvs\project-_1bmJiI5-py3.11\Lib\site-packages\cloudpathlib\cloudpath.py", line 559, in open
    self._refresh_cache(force_overwrite_from_cloud=force_overwrite_from_cloud)
  File "C:\Users\PawelGlusinski\AppData\Local\pypoetry\Cache\virtualenvs\project-_1bmJiI5-py3.11\Lib\site-packages\cloudpathlib\cloudpath.py", line 1121, in _refresh_cache
    self.download_to(self._local)
  File "C:\Users\PawelGlusinski\AppData\Local\pypoetry\Cache\virtualenvs\project-_1bmJiI5-py3.11\Lib\site-packages\cloudpathlib\cloudpath.py", line 900, in download_to
    return self.client._download_file(self, destination)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\PawelGlusinski\AppData\Local\pypoetry\Cache\virtualenvs\project-_1bmJiI5-py3.11\Lib\site-packages\cloudpathlib\azure\azblobclient.py", line 140, in _download_file
    with partial_local_path.open("wb") as data:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\PawelGlusinski\AppData\Local\Programs\Python\Python311\Lib\pathlib.py", line 1044, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  OSError: [Errno 22] Invalid argument: 'C:\\Users\\PAWELG~1\\AppData\\Local\\Temp\\tmpvqvdscqd\\path\\to\\a\\file:with:colons.part'

One solution that works for me as a workaround is to update _local method in CloudPath class

    @property
    def _local(self) -> Path:
        """Cached local version of the file."""
        return self.client._local_cache_dir / self._no_prefix.replace(":", "_")

@pjbull pjbull added the bug Something isn't working label Jun 6, 2024
@pjbull
Copy link
Member

pjbull commented Jun 6, 2024

Thanks @glusin. This looks like a bug we should fix.

Capturing a few thoughts on what we'll want from a solution:

  • Support different character sets for different OSes.
  • Sanitize self._no_prefix in the self._local property based on the OS we are caching to.
  • Make sanitization generally reversible (in case we need to recover original filename).
  • Don't introduce additional dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants