Our goal is to be the meringue of file management libraries: the subtle sweetness of
pathlib
working in harmony with the ethereal lightness of the cloud.
A Python library with classes that mimic pathlib.Path
's interface for URIs from different cloud storage services.
with CloudPath("s3://bucket/filename.txt").open("w+") as f:
f.write("Send my changes to the cloud!")
Why use cloudpathlib?
- Familiar: If you know how to interact with
Path
, you know how to interact withCloudPath
. All of the cloud-relevantPath
methods are implemented. - Supported clouds: AWS S3, Google Cloud Storage, and Azure Blob Storage are implemented. FTP is on the way.
- Extensible: The base classes do most of the work generically, so implementing two small classes
MyPath
andMyClient
is all you need to add support for a new cloud storage service. - Read/write support: Reading just works. Using the
write_text
,write_bytes
or.open('w')
methods will all upload your changes to cloud storage without any additional file management as a developer. - Seamless caching: Files are downloaded locally only when necessary. You can also easily pass a persistent cache folder so that across processes and sessions you only re-download what is necessary.
- Tested: Comprehensive test suite and code coverage.
- Testability: Local filesystem implementations that can be used to easily mock cloud storage in your unit tests.
Installation
cloudpathlib
depends on the cloud services' SDKs (e.g., boto3
, google-cloud-storage
, azure-storage-blob
) to communicate with their respective storage service. If you try to use cloud paths for a cloud service for which you don't have dependencies installed, cloudpathlib
will error and let you know what you need to install.
To install a cloud service's SDK dependency when installing cloudpathlib
, you need to specify it using pip's "extras" specification. For example:
pip install cloudpathlib[s3,gs,azure]
With some shells, you may need to use quotes:
pip install "cloudpathlib[s3,gs,azure]"
Currently supported cloud storage services are: azure
, gs
, s3
. You can also use all
to install all available services' dependencies.
If you do not specify any extras or separately install any cloud SDKs, you will only be able to develop with the base classes for rolling your own cloud path class.
conda
cloudpathlib
is also available using conda
from conda-forge. Note that to install the necessary cloud service SDK dependency, you should include the appropriate suffix in the package name. For example:
conda install cloudpathlib-s3 -c conda-forge
If no suffix is used, only the base classes will be usable. See the conda-forge/cloudpathlib-feedstock for all installation options.
Development version
You can get latest development version from GitHub:
pip install https://github.com/drivendataorg/cloudpathlib.git#egg=cloudpathlib[all]
Note that you similarly need to specify cloud service dependencies, such as all
in the above example command.
Quick usage
Here's an example to get the gist of using the package. By default, cloudpathlib
authenticates with the environment variables supported by each respective cloud service SDK. For more details and advanced authentication options, see the "Authentication" documentation.
from cloudpathlib import CloudPath
# dispatches to S3Path based on prefix
root_dir = CloudPath("s3://drivendata-public-assets/")
root_dir
#> S3Path('s3://drivendata-public-assets/')
# there's only one file, but globbing works in nested folder
for f in root_dir.glob('**/*.txt'):
text_data = f.read_text()
print(f)
print(text_data)
#> s3://drivendata-public-assets/odsc-west-2019/DATA_DICTIONARY.txt
#> Eviction Lab Data Dictionary
#>
#> Additional information in our FAQ evictionlab.org/help-faq/
#> Full methodology evictionlab.org/methods/
#>
#> ... (additional text output truncated)
# use / to join paths (and, in this case, create a new file)
new_file_copy = root_dir / "nested_dir/copy_file.txt"
new_file_copy
#> S3Path('s3://drivendata-public-assets/nested_dir/copy_file.txt')
# show things work and the file does not exist yet
new_file_copy.exists()
#> False
# writing text data to the new file in the cloud
new_file_copy.write_text(text_data)
#> 6933
# file now listed
list(root_dir.glob('**/*.txt'))
#> [S3Path('s3://drivendata-public-assets/nested_dir/copy_file.txt'),
#> S3Path('s3://drivendata-public-assets/odsc-west-2019/DATA_DICTIONARY.txt')]
# but, we can remove it
new_file_copy.unlink()
# no longer there
list(root_dir.glob('**/*.txt'))
#> [S3Path('s3://drivendata-public-assets/odsc-west-2019/DATA_DICTIONARY.txt')]
Supported methods and properties
Most methods and properties from pathlib.Path
are supported except for the ones that don't make sense in a cloud context. There are a few additional methods or properties that relate to specific cloud services or specifically for cloud paths.
Methods + properties | AzureBlobPath |
S3Path |
GSPath |
---|---|---|---|
absolute |
โ | โ | โ |
anchor |
โ | โ | โ |
as_uri |
โ | โ | โ |
drive |
โ | โ | โ |
exists |
โ | โ | โ |
glob |
โ | โ | โ |
is_absolute |
โ | โ | โ |
is_dir |
โ | โ | โ |
is_file |
โ | โ | โ |
is_relative_to |
โ | โ | โ |
iterdir |
โ | โ | โ |
joinpath |
โ | โ | โ |
match |
โ | โ | โ |
mkdir |
โ | โ | โ |
name |
โ | โ | โ |
open |
โ | โ | โ |
parent |
โ | โ | โ |
parents |
โ | โ | โ |
parts |
โ | โ | โ |
read_bytes |
โ | โ | โ |
read_text |
โ | โ | โ |
relative_to |
โ | โ | โ |
rename |
โ | โ | โ |
replace |
โ | โ | โ |
resolve |
โ | โ | โ |
rglob |
โ | โ | โ |
rmdir |
โ | โ | โ |
samefile |
โ | โ | โ |
stat |
โ | โ | โ |
stem |
โ | โ | โ |
suffix |
โ | โ | โ |
suffixes |
โ | โ | โ |
touch |
โ | โ | โ |
unlink |
โ | โ | โ |
with_name |
โ | โ | โ |
with_stem |
โ | โ | โ |
with_suffix |
โ | โ | โ |
write_bytes |
โ | โ | โ |
write_text |
โ | โ | โ |
as_posix |
โ | โ | โ |
chmod |
โ | โ | โ |
cwd |
โ | โ | โ |
expanduser |
โ | โ | โ |
group |
โ | โ | โ |
home |
โ | โ | โ |
is_block_device |
โ | โ | โ |
is_char_device |
โ | โ | โ |
is_fifo |
โ | โ | โ |
is_mount |
โ | โ | โ |
is_reserved |
โ | โ | โ |
is_socket |
โ | โ | โ |
is_symlink |
โ | โ | โ |
lchmod |
โ | โ | โ |
link_to |
โ | โ | โ |
lstat |
โ | โ | โ |
owner |
โ | โ | โ |
readlink |
โ | โ | โ |
root |
โ | โ | โ |
symlink_to |
โ | โ | โ |
clear_cache |
โ | โ | โ |
cloud_prefix |
โ | โ | โ |
copy |
โ | โ | โ |
copytree |
โ | โ | โ |
download_to |
โ | โ | โ |
etag |
โ | โ | โ |
fspath |
โ | โ | โ |
is_valid_cloudpath |
โ | โ | โ |
rmtree |
โ | โ | โ |
upload_from |
โ | โ | โ |
validate |
โ | โ | โ |
blob |
โ | โ | โ |
bucket |
โ | โ | โ |
container |
โ | โ | โ |
key |
โ | โ | โ |
md5 |
โ | โ | โ |
Icon made by srip from www.flaticon.com.
Sample code block generated using the reprexpy package.