I would like to build some functionality to move files between S3 and my local file system, but pathlib appears to combine repeated slashes, breaking my aws-cli functionality:
>>> from pathlib import Path
>>> str(Path('s3://loc'))
s3:/loc'
How can I manipulate S3 paths in this way?
s3path packageThe s3path package makes working with S3 paths a little less painful. It is installable from PyPI or conda-forge. Use the S3Path class for actual objects in S3 and otherwise use PureS3Path which shouldn't actually access S3.
Although the previous answer by metaperture did mention this package, it didn't include the URI syntax.
Also be aware that this package has certain deficiencies which are reported in its issues.
>>> from s3path import PureS3Path
>>> PureS3Path.from_uri('s3://mybucket/foo/bar') / 'add/me'
PureS3Path('/mybucket/foo/bar/add/me')
>>> _.as_uri()
's3://mybucket/foo/bar/add/me'
Note the instance relationships to pathlib:
>>> from pathlib import Path, PurePath
>>> from s3path import S3Path, PureS3Path
>>> isinstance(S3Path('/my-bucket/some/prefix'), Path)
True
>>> isinstance(PureS3Path('/my-bucket/some/prefix'), PurePath)
True
pathlib.PathThis is a lazier version of the answer by kichik using only pathlib. This approach is not necessarily recommended. It's just not always entirely necessary to use urllib.parse.
>>> from pathlib import Path
>>> orig_s3_path = 's3://mybucket/foo/bar'
>>> orig_path = Path(*Path(orig_s3_path).parts[1:])
>>> orig_path
PosixPath('mybucket/foo/bar')
>>> new_path = orig_path / 'add/me'
>>> new_s3_path = 's3://' + str(new_path)
>>> new_s3_path
's3://mybucket/foo/bar/add/me'
os.path.joinFor simple joins only, how about os.path.join?
>>> import os
>>> os.path.join('s3://mybucket/foo/bar', 'add/me')
's3://mybucket/foo/bar/add/me'
>>> os.path.join('s3://mybucket/foo/bar/', 'add/me')
's3://mybucket/foo/bar/add/me'
os.path.normpath cannot however be naively used:
>>> os.path.normpath('s3://mybucket/foo/bar') # Converts 's3://' to 's3:/'
's3:/mybucket/foo/bar'
You can try combining urllib.parse with pathlib.
from urllib.parse import urlparse, urlunparse
from pathlib import PosixPath
s3_url = urlparse('s3://bucket/key')
s3_path = PosixPath(s3_url.path)
s3_path /= 'hello'
s3_new_url = urlunparse((s3_url.scheme, s3_url.netloc, s3_path.as_posix(), s3_url.params, s3_url.query, s3_url.fragment))
print(s3_new_url)
It's quite cumbersome, but it's what you asked for.
cloudpathlibWanted to add this as another option that has nice caching and transparent read/write access in addition the the standard path manipulations.
The cloupathlib package provides pathlib methods support for S3 paths in addition to Google Cloud Storage and Azure Blob Storage.
For example:
from cloudpathlib import CloudPath
from itertools import islice
ladi = CloudPath("s3://ladi/Images/FEMA_CAP/2020/70349")
ladi.parent
#> S3Path('s3://ladi/Images/FEMA_CAP/2020')
ladi.bucket
#> 'ladi'
# list first 5 images for this incident
for p in islice(ladi.iterdir(), 5):
print(p)
#> s3://ladi/Images/FEMA_CAP/2020/70349/DSC_0001_5a63d42e-27c6-448a-84f1-bfc632125b8e.jpg
#> s3://ladi/Images/FEMA_CAP/2020/70349/DSC_0002_a89f1b79-786f-4dac-9dcc-609fb1a977b1.jpg
#> s3://ladi/Images/FEMA_CAP/2020/70349/DSC_0003_02c30af6-911e-4e01-8c24-7644da2b8672.jpg
#> s3://ladi/Images/FEMA_CAP/2020/70349/DSC_0004_d37c02b9-01a8-4672-b06f-2690d70e5e6b.jpg
#> s3://ladi/Images/FEMA_CAP/2020/70349/DSC_0005_d05609ce-1c45-4de3-b0f1-401c2bb3412c.jpg