cloudpathlib.S3Path¶
Bases: CloudPath
Class for representing and operating on AWS S3 URIs, in the style of the Python standard
library's pathlib module. Instances
represent a path in S3 with filesystem path semantics, and convenient methods allow for basic
operations like joining, reading, writing, iterating over contents, etc. This class almost
entirely mimics the pathlib.Path
interface, so most familiar properties and methods should be available and behave in the
expected way.
The S3Client class handles authentication with AWS. If a client instance is
not explicitly specified on S3Path instantiation, a default client is used. See S3Client's
documentation for more details.
Source code in cloudpathlib/s3/s3path.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
Attributes¶
anchor: str
property
¶
bucket: str
property
¶
The bucket name, or the full MRAP ARN for MRAP paths.
:type: :class:str
client: S3Client
instance-attribute
¶
cloud_prefix: str = 's3://'
class-attribute
instance-attribute
¶
drive: str
property
¶
etag
property
¶
fspath: str
property
¶
key: str
property
¶
name: str
property
¶
parent: Self
property
¶
parents: Tuple[Self, ...]
property
¶
parser: Self
property
¶
parts: Tuple[str, ...]
property
¶
stem: str
property
¶
suffix: str
property
¶
suffixes: List[str]
property
¶
Methods:¶
__init__(cloud_path: Union[str, Self, CloudPath], *parts: str, client: Optional[Client] = None) -> None
¶
Source code in cloudpathlib/cloudpath.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
absolute() -> Self
¶
Source code in cloudpathlib/cloudpath.py
1036 1037 | |
as_uri() -> str
¶
Source code in cloudpathlib/cloudpath.py
480 481 | |
as_url(presign: bool = False, expire_seconds: int = 60 * 60) -> str
¶
Source code in cloudpathlib/cloudpath.py
462 463 464 465 466 467 | |
clear_cache()
¶
Removes cache if it exists
Source code in cloudpathlib/cloudpath.py
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 | |
copy(target: Union[str, os.PathLike, Self], follow_symlinks: bool = True, preserve_metadata: bool = False, force_overwrite_to_cloud: Optional[bool] = None) -> Union[Path, Self]
¶
copy(
target: Self,
follow_symlinks=True,
preserve_metadata=False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Self
copy(
target: Path,
follow_symlinks=True,
preserve_metadata=False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Path
copy(
target: str,
follow_symlinks=True,
preserve_metadata=False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Union[Path, CloudPath]
Copy self to target folder or file, if self is a file.
Source code in cloudpathlib/cloudpath.py
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 | |
copy_into(target_dir: Union[str, os.PathLike, Self], follow_symlinks: bool = True, preserve_metadata: bool = False, force_overwrite_to_cloud: Optional[bool] = None) -> Union[Path, Self]
¶
copy_into(
target_dir: Self,
follow_symlinks: bool = True,
preserve_metadata: bool = False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Self
copy_into(
target_dir: Path,
follow_symlinks: bool = True,
preserve_metadata: bool = False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Path
copy_into(
target_dir: str,
follow_symlinks: bool = True,
preserve_metadata: bool = False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Union[Path, CloudPath]
Copy self into target directory, preserving the filename.
Source code in cloudpathlib/cloudpath.py
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 | |
copytree(destination, force_overwrite_to_cloud=None, ignore=None)
¶
copytree(
destination: Self,
force_overwrite_to_cloud: Optional[bool] = None,
ignore: Optional[
Callable[[str, Iterable[str]], Container[str]]
] = None,
) -> Self
copytree(
destination: Path,
force_overwrite_to_cloud: Optional[bool] = None,
ignore: Optional[
Callable[[str, Iterable[str]], Container[str]]
] = None,
) -> Path
copytree(
destination: Union[str, os.PathLike, Self],
force_overwrite_to_cloud: Optional[bool] = None,
ignore: Optional[
Callable[[str, Iterable[str]], Container[str]]
] = None,
) -> Union[Path, CloudPath]
Copy self to a directory, if self is a directory.
Source code in cloudpathlib/cloudpath.py
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 | |
download_to(destination: Union[str, os.PathLike]) -> Path
¶
Source code in cloudpathlib/cloudpath.py
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 | |
exists(follow_symlinks=True) -> bool
¶
Source code in cloudpathlib/cloudpath.py
483 484 | |
from_uri(uri: str) -> Self
classmethod
¶
Source code in cloudpathlib/cloudpath.py
496 497 498 | |
full_match(pattern: str, case_sensitive: Optional[bool] = None) -> bool
¶
Source code in cloudpathlib/cloudpath.py
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 | |
glob(pattern: Union[str, os.PathLike], case_sensitive: Optional[bool] = None, recurse_symlinks: bool = True) -> Generator[Self, None, None]
¶
Source code in cloudpathlib/cloudpath.py
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | |
info() -> CloudPathInfo
¶
Return a CloudPathInfo object for this path.
Source code in cloudpathlib/cloudpath.py
1183 1184 1185 | |
is_absolute() -> bool
¶
Source code in cloudpathlib/cloudpath.py
1039 1040 | |
is_dir(follow_symlinks=True) -> bool
¶
Source code in cloudpathlib/cloudpath.py
486 487 | |
is_file(follow_symlinks=True) -> bool
¶
Source code in cloudpathlib/cloudpath.py
489 490 | |
is_junction()
¶
Source code in cloudpathlib/cloudpath.py
983 984 | |
is_relative_to(other: Self) -> bool
¶
Source code in cloudpathlib/cloudpath.py
1062 1063 1064 1065 1066 1067 | |
is_valid_cloudpath(path: Union[str, CloudPath], raise_on_error: bool = False) -> Union[bool, TypeGuard[Self]]
classmethod
¶
is_valid_cloudpath(
path: CloudPath, raise_on_error: bool = ...
) -> TypeGuard[Self]
is_valid_cloudpath(
path: str, raise_on_error: bool = ...
) -> bool
Source code in cloudpathlib/cloudpath.py
363 364 365 366 367 368 369 370 371 372 373 374 | |
iterdir() -> Generator[Self, None, None]
¶
Source code in cloudpathlib/cloudpath.py
601 602 603 604 | |
joinpath(*pathsegments: Union[str, os.PathLike]) -> Self
¶
Source code in cloudpathlib/cloudpath.py
1033 1034 | |
match(path_pattern: str, case_sensitive: Optional[bool] = None) -> bool
¶
Source code in cloudpathlib/cloudpath.py
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 | |
mkdir(parents=False, exist_ok=False, mode: Optional[Any] = None, *, parent_mode=None)
¶
Source code in cloudpathlib/s3/s3path.py
47 48 49 50 51 | |
move(target: Union[str, os.PathLike, Self], follow_symlinks: bool = True, preserve_metadata: bool = False, force_overwrite_to_cloud: Optional[bool] = None) -> Union[Path, Self]
¶
move(
target: Self,
follow_symlinks: bool = True,
preserve_metadata: bool = False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Self
move(
target: Path,
follow_symlinks: bool = True,
preserve_metadata: bool = False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Path
move(
target: str,
follow_symlinks: bool = True,
preserve_metadata: bool = False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Union[Path, CloudPath]
Move self to target location, removing the source.
Source code in cloudpathlib/cloudpath.py
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 | |
move_into(target_dir: Union[str, os.PathLike, Self], follow_symlinks: bool = True, preserve_metadata: bool = False, force_overwrite_to_cloud: Optional[bool] = None) -> Union[Path, Self]
¶
move_into(
target_dir: Self,
follow_symlinks: bool = True,
preserve_metadata: bool = False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Self
move_into(
target_dir: Path,
follow_symlinks: bool = True,
preserve_metadata: bool = False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Path
move_into(
target_dir: str,
follow_symlinks: bool = True,
preserve_metadata: bool = False,
force_overwrite_to_cloud: Optional[bool] = None,
) -> Union[Path, CloudPath]
Move self into target directory, preserving the filename and removing the source.
Source code in cloudpathlib/cloudpath.py
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 | |
open(mode: str = 'r', buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, force_overwrite_from_cloud: Optional[bool] = None, force_overwrite_to_cloud: Optional[bool] = None) -> IO[Any]
¶
open(
mode: OpenTextMode = "r",
buffering: int = -1,
encoding: Optional[str] = None,
errors: Optional[str] = None,
newline: Optional[str] = None,
force_overwrite_from_cloud: Optional[bool] = None,
force_overwrite_to_cloud: Optional[bool] = None,
) -> TextIOWrapper
open(
mode: OpenBinaryMode,
buffering: Literal[0],
encoding: None = None,
errors: None = None,
newline: None = None,
force_overwrite_from_cloud: Optional[bool] = None,
force_overwrite_to_cloud: Optional[bool] = None,
) -> FileIO
open(
mode: OpenBinaryModeUpdating,
buffering: Literal[-1, 1] = -1,
encoding: None = None,
errors: None = None,
newline: None = None,
force_overwrite_from_cloud: Optional[bool] = None,
force_overwrite_to_cloud: Optional[bool] = None,
) -> BufferedRandom
open(
mode: OpenBinaryModeWriting,
buffering: Literal[-1, 1] = -1,
encoding: None = None,
errors: None = None,
newline: None = None,
force_overwrite_from_cloud: Optional[bool] = None,
force_overwrite_to_cloud: Optional[bool] = None,
) -> BufferedWriter
open(
mode: OpenBinaryModeReading,
buffering: Literal[-1, 1] = -1,
encoding: None = None,
errors: None = None,
newline: None = None,
force_overwrite_from_cloud: Optional[bool] = None,
force_overwrite_to_cloud: Optional[bool] = None,
) -> BufferedReader
open(
mode: OpenBinaryMode,
buffering: int = -1,
encoding: None = None,
errors: None = None,
newline: None = None,
force_overwrite_from_cloud: Optional[bool] = None,
force_overwrite_to_cloud: Optional[bool] = None,
) -> BinaryIO
open(
mode: str,
buffering: int = -1,
encoding: Optional[str] = None,
errors: Optional[str] = None,
newline: Optional[str] = None,
force_overwrite_from_cloud: Optional[bool] = None,
force_overwrite_to_cloud: Optional[bool] = None,
) -> IO[Any]
Source code in cloudpathlib/cloudpath.py
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 | |
read_bytes() -> bytes
¶
Source code in cloudpathlib/cloudpath.py
970 971 972 | |
read_text(encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None) -> str
¶
Source code in cloudpathlib/cloudpath.py
974 975 976 977 978 979 980 981 | |
relative_to(other: Self, walk_up: bool = False) -> PurePosixPath
¶
Source code in cloudpathlib/cloudpath.py
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 | |
rename(target: Self) -> Self
¶
Source code in cloudpathlib/cloudpath.py
910 911 912 913 | |
replace(target: Self) -> Self
¶
Source code in cloudpathlib/cloudpath.py
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 | |
resolve(strict: bool = False) -> Self
¶
Source code in cloudpathlib/cloudpath.py
1042 1043 | |
rglob(pattern: Union[str, os.PathLike], case_sensitive: Optional[bool] = None, recurse_symlinks: bool = True) -> Generator[Self, None, None]
¶
Source code in cloudpathlib/cloudpath.py
586 587 588 589 590 591 592 593 594 595 596 597 598 599 | |
rmdir() -> None
¶
Source code in cloudpathlib/cloudpath.py
915 916 917 918 919 920 921 922 923 924 925 926 927 | |
rmtree() -> None
¶
Delete an entire directory tree.
Source code in cloudpathlib/cloudpath.py
1213 1214 1215 1216 1217 1218 1219 | |
samefile(other_path: Union[str, os.PathLike]) -> bool
¶
Source code in cloudpathlib/cloudpath.py
929 930 931 | |
stat(follow_symlinks=True)
¶
Source code in cloudpathlib/s3/s3path.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
touch(exist_ok: bool = True, mode: Optional[Any] = None)
¶
Source code in cloudpathlib/s3/s3path.py
53 54 55 56 57 58 59 60 61 62 63 64 65 | |
unlink(missing_ok: bool = True) -> None
¶
Source code in cloudpathlib/cloudpath.py
933 934 935 936 937 938 939 | |
upload_from(source: Union[str, os.PathLike], force_overwrite_to_cloud: Optional[bool] = None) -> Self
¶
Upload a file or directory to the cloud path.
Source code in cloudpathlib/cloudpath.py
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 | |
validate(v: str) -> Self
classmethod
¶
Used as a Pydantic validator. See https://docs.pydantic.dev/2.0/usage/types/custom/
Source code in cloudpathlib/cloudpath.py
1733 1734 1735 1736 1737 | |
walk(top_down: bool = True, on_error: Optional[Callable] = None, follow_symlinks: bool = False, lazy: bool = False) -> Generator[Tuple[Self, List[str], List[str]], None, None]
¶
Walk the directory tree rooted at this path, similar to
:meth:pathlib.Path.walk and :func:os.walk.
By default (lazy=False) the entire subtree is listed up front with a
single recursive listing, which is the fastest option for a full walk on
cloud backends.
Pass lazy=True to defer listing until each directory is visited (one
non-recursive listing per directory). This is slower for a full walk,
but lets callers prune dirnames in place (when top_down=True) to
avoid fetching the contents of skipped subtrees entirely -- which can be
dramatically faster and cheaper for sparse traversals.
follow_symlinks is accepted for signature parity with pathlib
but has no effect on cloud backends, which do not have symlinks.
Source code in cloudpathlib/cloudpath.py
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | |
with_name(name: str) -> Self
¶
Source code in cloudpathlib/cloudpath.py
1139 1140 | |
with_segments(*pathsegments) -> Self
¶
Create a new CloudPath with the same client out of the given segments. The first segment will be interpreted as the bucket/container name.
Source code in cloudpathlib/cloudpath.py
1142 1143 1144 1145 1146 | |
with_stem(stem: str) -> Self
¶
Source code in cloudpathlib/cloudpath.py
1136 1137 | |
with_suffix(suffix: str) -> Self
¶
Source code in cloudpathlib/cloudpath.py
1148 1149 | |
write_bytes(data: bytes) -> int
¶
Open the file in bytes mode, write to it, and close the file.
NOTE: vendored from pathlib since we override open https://github.com/python/cpython/blob/3.8/Lib/pathlib.py#L1235-L1242
Source code in cloudpathlib/cloudpath.py
941 942 943 944 945 946 947 948 949 950 | |
write_text(data: str, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None) -> int
¶
Open the file in text mode, write to it, and close the file.
NOTE: vendored from pathlib since we override open https://github.com/python/cpython/blob/3.10/Lib/pathlib.py#L1146-L1155
Source code in cloudpathlib/cloudpath.py
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 | |