s3pathlib

2.3.6 · active · verified Sun Apr 12

s3pathlib is a Python package that provides an intuitive, object-oriented programming (OOP) interface for manipulating AWS S3 objects and directories. Its API closely resembles the Python standard library's `pathlib` module, making S3 interactions feel familiar and Pythonic. The library is actively maintained, with the current version being 2.3.6, and receives regular minor updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `s3pathlib` with AWS credentials, create an `S3Path` object, write content to S3, and read it back. It shows how to use `context.attach_boto_session` for explicit credential management.

import boto3
import os
from s3pathlib import S3Path, context

# Configure AWS credentials (e.g., from environment variables or a profile)
# In a real application, consider using AWS IAM roles or proper credential management.
aws_region = os.environ.get("AWS_REGION", "us-east-1")
aws_profile = os.environ.get("AWS_PROFILE", None)

# Attach a boto3 session for s3pathlib to use
if aws_profile:
    session = boto3.session.Session(region_name=aws_region, profile_name=aws_profile)
else:
    session = boto3.session.Session(region_name=aws_region)
context.attach_boto_session(session)

# Define an S3 path object
bucket_name = os.environ.get("S3_BUCKET_NAME", "your-test-bucket-12345")
s3_path = S3Path(bucket_name, "my-folder", "hello.txt")

# Example: Write text to S3
print(f"Writing to {s3_path.uri}...")
s3_path.write_text("Hello, s3pathlib!")
print("Content written.")

# Example: Read text from S3
if s3_path.exists():
    content = s3_path.read_text()
    print(f"Content read from S3: '{content}'")

# Example: Check if a folder exists and list its contents
s3_dir = S3Path(bucket_name, "my-folder/")
print(f"Checking if {s3_dir.uri} exists: {s3_dir.exists()}")

# Clean up (optional)
# s3_path.delete() # Deletes the object
# s3_dir.delete_dir() # Deletes all objects under the prefix (careful!)

# Detach the boto3 session when done (optional, for explicit resource management)
context.detach_boto_session()

view raw JSON →