S3 navigation using object, inspired by pathlib.Path
Project description
Installation
pip install pathlibs3
Usage
Create a PathlibS3 Object
import boto3
from pathlibs3.pathlibs3 import S3Path
# Create a pathlibs3
client = boto3.client("s3", region_name="us-east-1")
bucket = "test-bucket"
# Create an object to s3://test-bucket/myfolder
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
# You can also concatenate object
# Create an object to s3://test-bucket/myfile/random_file.txt
s3_path_to_random_file = s3_path_to_myfolder / "random_file.txt"
Iter over a directory
# Iter over this directory
for path in s3_path.iterdir():
print(path)
# Iter over this directory recursively
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
# Create an pathlibs3 object
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
# Create a pathlib object
local_path = Path("/tmp/local_folder")
# Will download the s3 folder localy
S3Path.copy(s3_path_to_myfolder, local_path)
# You may also use string for local path
# Example: copy from s3 to local dir using string
S3Path.copy(s3_path_to_myfolder, "/tmp/local_folder")
Copy from local to s3
# Create an pathlibs3 object
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
# Create a pathlib object
local_path = Path("/tmp/local_folder")
# Will download the s3 folder localy
S3Path.copy(local_path, s3_path_to_myfolder)
Copy from s3 to s3
# Create an pathlibs3 object
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
# Create another pathlibs3 object
s3_path_to_anotherfolder = S3Path(client, bucket, "anotherfolder/")
# Will download the s3 folder localy
S3Path.copy(s3_path_to_myfolder, s3_path_to_anotherfolder)
Delete a folder
# Create an pathlibs3 object
s3_path_to_myfolder = S3Path(client, bucket, "myfolder/")
s3_path_to_myfolder.delete()
Move a folder
# Create an pathlibs3 object
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pathlibs3-1.1.8.tar.gz
(3.8 kB
view details)
Built Distribution
File details
Details for the file pathlibs3-1.1.8.tar.gz
.
File metadata
- Download URL: pathlibs3-1.1.8.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.8.0-47-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff7cd806d04093b9d7d42330d8361f29803880b6bc9f0d16607a56e258d5dc15 |
|
MD5 | 69db0b82125a5b541fd92d0f1dac652c |
|
BLAKE2b-256 | 6275e636b334111776de6325794ab63cce8abde3b714d5a46d1637abbc46d842 |
File details
Details for the file pathlibs3-1.1.8-py3-none-any.whl
.
File metadata
- Download URL: pathlibs3-1.1.8-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.8.0-47-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57311c1235a31d52eed5bff67e3a3f38f6f1034df2e0bf8946b86fa627a02b43 |
|
MD5 | 42b0f531784fa09473d43934dce904d3 |
|
BLAKE2b-256 | ad179efb580f3d866500f17a421a8287d3c1def1054b9a7f30780cd7e26d7d1d |