s3path
s3path offers a Pythonic, object-oriented interface for working with AWS S3 objects and directories, mirroring the standard library's `pathlib` module. It seamlessly integrates with `boto3` to provide a convenient filesystem-like experience for S3 buckets. The current version is 0.6.5, with consistent updates and patch releases.
Warnings
- breaking Version 0.6.0 removed support for Python 3.8 and introduced support for Python 3.14. Projects still on Python 3.8 will need to upgrade their Python version before upgrading s3path.
- breaking From version 0.6.0, the `glob` and `rglob` methods exclusively use a new, optimized algorithm. The `glob_new_algorithm` configuration parameter, previously used to switch algorithms, is now deprecated and its functionality removed.
- gotcha The `.exists()` method in versions prior to 0.6.5 could return `True` for partial key matches (e.g., querying for 'foo' would return true if 'foobar' existed). This was fixed in 0.6.5 to correctly use `ListObjectsV2`.
- gotcha A caching system for the `is_dir` method was introduced in 0.6.2 but subsequently reverted and removed in 0.6.3 due to issues. Relying on this caching for performance in intermediate 0.6.2 releases is not advised.
- gotcha The `walk` method can be very heavy on AWS S3 API calls, potentially leading to increased costs and slower performance, especially for large buckets. It is generally recommended to use recursive `glob` instead for most traversal needs.
- deprecated The `glob_new_algorithm` configuration parameter, used to switch glob algorithms, entered a deprecation cycle in version 0.6.0 as the new algorithm became the sole implementation.
- breaking Version 0.6.3 fixed a null encryption key vulnerability. Older versions might be susceptible to this security flaw.
Install
-
pip install s3path
Imports
- S3Path
from s3path import S3Path
- PureS3Path
from s3path import PureS3Path
- VersionedS3Path
from s3path import VersionedS3Path
- configuration_map
from s3path import configuration_map
Quickstart
import os
import boto3
from s3path import S3Path
# Configure boto3 session (recommended for programmatic access)
# Replace with your actual region and credentials or profile
# For local testing, ensure AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set
boto3.setup_default_session(
region_name=os.environ.get('AWS_REGION', 'us-east-1'),
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', 'test'),
aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', 'test')
)
bucket_name = 'your-s3-bucket'
file_key = 'my-folder/my-file.txt'
# Create an S3Path object
s3_path = S3Path(f'/{bucket_name}/{file_key}')
print(f"S3 Path: {s3_path}")
# Check if the file exists (requires S3 interaction)
if not s3_path.exists():
print(f"Creating file: {s3_path}")
s3_path.write_text('Hello from s3path!')
print("File created.")
else:
print(f"File '{s3_path}' already exists.")
# Read content
content = s3_path.read_text()
print(f"Content of '{s3_path}': {content}")
# List contents of a directory-like prefix
directory_path = S3Path(f'/{bucket_name}/my-folder/')
print(f"\nListing contents of '{directory_path}':")
for p in directory_path.iterdir():
print(f" - {p}")
# Clean up (optional)
# s3_path.unlink() # Uncomment to delete the file
# print(f"Deleted {s3_path}")