我想构建一些功能来在 S3 和我的本地文件系统之间移动文件,但pathlib
似乎结合了重复的斜杠,破坏了我的 aws-cli 功能:
>>> from pathlib import Path
>>> str(Path('s3://loc'))
s3:/loc'
如何以这种方式操作 S3 路径?
s3path
包该s3path
软件包使使用 S3 路径变得不那么痛苦。它可以从PyPI或conda- forge 安装。将S3Path
类用于 S3 中的实际对象,否则使用PureS3Path
不应实际访问 S3 的类。
尽管metaperture 之前的回答确实提到了这个包,但它没有包含 URI 语法。
另请注意,此软件包存在某些缺陷,这些缺陷已在其问题中报告。
>>> 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'
请注意与 的实例关系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.Path
这是 kichik 使用 only 的答案的懒惰版本pathlib
。不一定推荐这种方法。只是并不总是完全需要使用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.join
仅对于简单的连接,怎么样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
但是不能天真地使用:
>>> os.path.normpath('s3://mybucket/foo/bar') # Converts 's3://' to 's3:/'
's3:/mybucket/foo/bar'
您可以尝试将urllib.parse与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)
这很麻烦,但这是你要求的。
这是一个为 s3 路径子类化 pathlib.Path 的模块:https ://pypi.org/project/s3path/
用法:
>>> from s3path import S3Path
>>> bucket_path = S3Path('/pypi-proxy/')
>>> [path for path in bucket_path.iterdir() if path.is_dir()]
[S3Path('/pypi-proxy/requests/'),
S3Path('/pypi-proxy/boto3/'),
S3Path('/pypi-proxy/botocore/')]
>>> boto3_package_path = S3Path('/pypi-proxy/boto3/boto3-1.4.1.tar.gz')
>>> boto3_package_path.exists()
True
>>> boto3_package_path.is_dir()
False
>>> boto3_package_path.is_file()
True
>>> botocore_index_path = S3Path('/pypi-proxy/botocore/index.html')
>>> with botocore_index_path.open() as f:
>>> print(f.read())
"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Package Index</title>
</head>
<body>
<a href="botocore-1.4.93.tar.gz">botocore-1.4.93.tar.gz</a><br>
</body>
</html>
"""
cloudpathlib
想要将此添加为另一个选项,除了标准路径操作之外,它还具有良好的缓存和透明的读/写访问权限。
除了 Google Cloud Storage 和 Azure Blob Storage 之外,该cloupathlib
包还为 S3 路径提供pathlib 方法支持。
例如:
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
扩展 str 类来处理这个问题既有用又简单
class URL(str):
def __truediv__(self, val):
return URL(self + '/' + val)
一个示例用法是URL('s3://mybucket') / 'test'
→"s3://mybucket/test"
No. pathlib
用于文件系统路径(即计算机上文件的路径),而 S3 路径是 URI。
我同意@jwodder 的回答,pathlib 仅适用于 fs 路径。但是,出于好奇,我尝试了从 pathlib.Path 继承的一些问题,并获得了一个相当可行的解决方案。
import pathlib
class S3Path(pathlib.PosixPath):
s3_schema = "s3:/"
def __new__(cls, *args, **kwargs):
if args[0].startswith(cls.s3_schema):
args = (args[0].replace(cls.s3_schema, "", 1),) + args[1:]
return super().__new__(cls, *args, **kwargs)
def __str__(self):
try:
return self.s3_schema + self._str
except AttributeError:
self._str = (
self._format_parsed_parts(
self._drv,
self._root,
self._parts,
)
or "."
)
return self.s3_schema + self._str
def test_basic():
s3_path_str: str = "s3://some/location"
s3_path = S3Path(s3_path_str)
assert str(s3_path) == s3_path_str
s3_path_1 = s3_path / "here"
assert str(s3_path_1) == s3_path_str + "/here"
assert s3_path.parent == S3Path("s3://some")
它的优点是您不需要任何 pip install 依赖项。另外,您可以轻松地将其应用于任何其他 URI,例如路径,例如 hdfs
Pathy 非常适合这个: https ://github.com/justindujardin/pathy
它在后台使用 Smart open 来提供对存储桶存储的文件访问,因此比 s3path 更好。
您可以Pathy.fluid
用来制作适用于本地文件和存储桶存储文件的 api
from pathlib import BasePath
from pathy import Pathy, FluidPath
def process_f(f: Union[Union[str, Pathy, BasePath]):
path = Pathy.fluid(f)
# now you have a Pathlib you can process that's local or in s3/GCS