Installation
pip install pathlibs3
Usage
Create a PathlibS3 Object
import boto3
from pathlibs3.pathlibs3 import S3Path
client = boto3.client("s3", region_name="us-east-1")
bucket = "test-bucket"
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
s3_path_to_random_file = s3_path_to_myfolder / "random_file.txt"
Iter over a directory
for path in s3_path.iterdir():
print(path)
for path in s3_path.iterdir(recursive=True):
print(path)
Use classic pathlib.Path function
parent and parents
>> s3_path_to_myfolder = S3Path(client, bucket, "myfolder/folder1/folder2")
>> s3_path_to_myfolder.parent
S3Path(client, bucket, "myfolder/folder1")
>> s3_path_to_myfolder.parents
[S3Path(client, bucket, "myfolder/folder1"), S3Path(client, bucket, "myfolder")]
name
>> s3_path_to_myfolder = S3Path(client, bucket, "myfolder/folder1/folder2/test.txt")
>> s3_path_to_myfolder.name
"test.txt"
exists
>> s3_path_to_myfolder = S3Path(client, bucket, "myfolder/folder1/folder2/test.txt")
>> s3_path_to_myfolder.exists()
True
Copy file or folder
Copy from s3 to local
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
local_path = Path("/tmp/local_folder")
S3Path.copy(s3_path_to_myfolder, local_path)
S3Path.copy(s3_path_to_myfolder, "/tmp/local_folder")
Copy from local to s3
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
local_path = Path("/tmp/local_folder")
S3Path.copy(local_path, s3_path_to_myfolder)
Copy from s3 to s3
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
s3_path_to_anotherfolder = S3Path(client, bucket, "anotherfolder/")
S3Path.copy(s3_path_to_myfolder, s3_path_to_anotherfolder)
Delete a folder
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
s3_path_to_myfolder.delete()
Move a folder
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
s3_path_other_folder = S3Path(client, bucket, "myotherfolder/")
S3Path.move(s3_path_to_myfolder, s3_path_other_folder)
Contribution
run test
run test with poetry run python -m pytest