Registry / aws / s3torchconnector

s3torchconnector

JSON →
library1.5.0pypypi✓ verified 85d ago

S3TorchConnector provides an efficient integration for PyTorch `Dataset` and `DataLoader` to stream data directly from Amazon S3. It enables training machine learning models on S3-resident data without needing to download it locally, optimized for large-scale and distributed workloads. The current version is 1.5.0, with releases typically aligning with PyTorch and AWS SDK updates.

pip install s3torchconnector
INSTALL
IMPORT
SIG · S3TORCHCONNECTOR
S
s3torchconnector
awspythonv1.5.0
Install
68.4s avg
Import
5806ms
Disk
4787MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 77.08s
py 3.11
✕ build_error
✓ 69.93s
py 3.12
✕ build_error
✓ 64.63s
py 3.13
✕ build_error
✓ 61.8s
py 3.9
✕ build_error
✕ timeout
4787MB installed
● package 4787MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

S3MapDataset
from s3torchconnector import S3MapDataset
S3IterableDataset
from s3torchconnector import S3IterableDataset
S3FilePipe
from s3torchconnector import S3FilePipe
S3Dataset
from s3torchconnector import S3MapDataset # or S3IterableDataset
from s3torchconnector.s3dataset import S3Dataset
The `S3Dataset` class was deprecated in early versions and replaced by `S3MapDataset` and `S3IterableDataset`.

This quickstart demonstrates how to use both `S3MapDataset` and `S3IterableDataset` with PyTorch's `DataLoader`. It shows how to initialize them with an S3 URI, iterate through data, and includes error handling for common S3 access issues. Ensure your AWS credentials are configured and the specified S3 bucket/prefix contains data.

import torch from torch.utils.data import DataLoader from s3torchconnector import S3MapDataset, S3IterableDataset import os import io # --- Configuration for S3 Access --- # Ensure your AWS credentials are configured (e.g., via AWS CLI, environment variables, or IAM roles). # Example environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION. # # IMPORTANT: Replace 's3torchconnector-example-bucket' and 'quickstart-data/' with your # actual S3 bucket and prefix. The bucket should contain some files (e.g., text files) # for the example to successfully load data. S3_BUCKET = os.environ.get('S3_QUICKSTART_BUCKET', 's3torchconnector-example-bucket') S3_PREFIX = os.environ.get('S3_QUICKSTART_PREFIX', 'quickstart-data/') S3_URI = f"s3://{S3_BUCKET}/{S3_PREFIX}" print(f"Attempting to connect to S3 URI: {S3_URI}") print("Please ensure this bucket/prefix exists and contains data, and your AWS credentials are configured.") print("If you encounter 'Forbidden' or 'NoCredentialsError', check your AWS setup.") # --- S3MapDataset Example --- # S3MapDataset first lists all objects under the given S3 URI prefix, then allows indexed access. # Suitable when you need a fixed-size dataset and random access. try: print("\n--- S3MapDataset Demonstration ---") map_dataset = S3MapDataset(S3_URI) print(f"S3MapDataset initialized. Found {len(map_dataset)} objects.") if len(map_dataset) > 0: # Accessing an item by index item_data = map_dataset[0] # Returns a file-like object (BytesIO by default) if isinstance(item_data, io.BytesIO): content_sample = item_data.read(100).decode('utf-8', errors='ignore') # Read first 100 bytes print(f"Sample from first item (S3MapDataset): '{content_sample}'...") else: print(f"First item type: {type(item_data)}") # Using DataLoader with S3MapDataset map_dataloader = DataLoader(map_dataset, batch_size=2, num_workers=0) # num_workers=0 for simplicity print("Iterating through S3MapDataset with DataLoader:") for i, batch in enumerate(map_dataloader): print(f"MapDataset Batch {i}: {len(batch)} items.") if len(batch) > 0 and isinstance(batch[0], io.BytesIO): print(f" First item in batch content sample: {batch[0].read(30).decode('utf-8', errors='ignore')}...") if i >= 1: # Limit iterations for a quick example break else: print("S3MapDataset found no objects. Please ensure your S3 bucket/prefix contains files.") except Exception as e: print(f"Error during S3MapDataset example: {e}") print("Ensure AWS credentials are valid and the S3 path exists and is accessible.") # --- S3IterableDataset Example --- # S3IterableDataset streams objects one by one as they are iterated. # Suitable for very large datasets where listing all objects upfront is too slow or memory intensive. try: print("\n--- S3IterableDataset Demonstration ---") iterable_dataset = S3IterableDataset(S3_URI) # Using DataLoader with S3IterableDataset # For num_workers > 0, consider using a `worker_init_fn` for proper distributed data loading. iterable_dataloader = DataLoader(iterable_dataset, batch_size=2, num_workers=0) print("Iterating through S3IterableDataset with DataLoader:") for i, batch in enumerate(iterable_dataloader): print(f"IterableDataset Batch {i}: {len(batch)} items.") if len(batch) > 0 and isinstance(batch[0], io.BytesIO): print(f" First item in batch content sample: {batch[0].read(30).decode('utf-8', errors='ignore')}...") if i >= 1: # Limit iterations break print("S3IterableDataset iteration complete (limited for quickstart).") except Exception as e: print(f"Error during S3IterableDataset example: {e}") print("Ensure AWS credentials are valid and the S3 path exists and is accessible.") print("\nQuickstart examples concluded.")
Debug
Known issues
deprecatedThe `S3Dataset` class was deprecated in earlier versions (pre-1.0.0) and has been removed in favor of `S3MapDataset` and `S3IterableDataset` for clearer semantics.
fix
Migrate to `S3MapDataset` for map-style access (indexed, random access) or `S3IterableDataset` for iterable-style streaming access. Both are imported directly from `s3torchconnector`.
affects: <1.0.0
gotchaIncorrect AWS IAM permissions or missing credentials lead to `Forbidden` or `NoCredentialsError` when accessing S3 buckets.
fix
Ensure your IAM user/role has `s3:ListBucket` (for prefixes), `s3:GetObject` (to read objects), and `s3:HeadObject` (for object metadata) permissions for the target bucket and prefix. Configure AWS credentials via environment variables (e.g., `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`), AWS CLI config, or IAM instance profiles/roles.
affects: All versions
gotchaHigh latency and increased cost can occur when loading a very large number of small files from S3 due to per-file overhead for each `GetObject` call.
fix
Consider combining small files into larger archives (e.g., tar files, TFRecords, Parquet) on S3. If listing all files upfront is too slow, `S3IterableDataset` can stream data without needing a full listing, but the per-file overhead remains for retrieval.
affects: All versions
gotcha`S3IterableDataset` when used with `torch.utils.data.DataLoader` and `num_workers > 0` requires a `worker_init_fn` to ensure proper data distribution and prevent duplicate or missed data across workers.
fix
Implement a `worker_init_fn` to initialize each worker's dataset instance with a unique seed or offset, often utilizing `torch.utils.data.get_worker_info()` and potentially `S3Checkpoint` for resumable training. Refer to the official `s3torchconnector` documentation on worker initialization.
affects: All versions
Errors
Common errors & fixes
botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
AWS IAM user/role lacks necessary permissions (`s3:GetObject`, `s3:ListBucket`) for the specified S3 bucket or objects.
fix
Review and update your IAM policy to grant `s3:ListBucket` for the bucket (and its prefix) and `s3:GetObject` for objects within the prefix. Also, ensure the bucket policy doesn't explicitly deny access.
botocore.exceptions.NoCredentialsError: Unable to locate credentials
AWS SDK for Python (boto3) cannot find any configured AWS credentials to authenticate with S3.
fix
Configure AWS credentials. Common methods include setting environment variables (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_REGION`), configuring the AWS CLI (`aws configure`), or using an IAM role for EC2 instances/EKS pods.
botocore.exceptions.ClientError: An error occurred (404) when calling the HeadObject operation: Not Found
The specified S3 bucket name or object prefix is incorrect, or the configured AWS region does not contain the bucket.
fix
Double-check the S3 URI (`s3://bucket/prefix`) for typos. Verify the bucket name and prefix are correct. Ensure your AWS region (e.g., `AWS_REGION` environment variable or boto3 client configuration) matches the region where the S3 bucket exists.
ModuleNotFoundError: No module named 's3torchconnector.s3dataset'
Attempting to import the old, deprecated `S3Dataset` class which has been removed from the library's public API.
fix
Replace `S3Dataset` with `S3MapDataset` or `S3IterableDataset` from the top-level `s3torchconnector` package. For example, change `from s3torchconnector.s3dataset import S3Dataset` to `from s3torchconnector import S3MapDataset`.
Upgrade
Version history
1.5.0latest on PyPI · released Feb 20, 2026
Audit
Dependencies
torchrequiredCore PyTorch library, provides Dataset and DataLoader abstractions.
boto3requiredAWS SDK for Python, used for interacting with S3.
Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources
s3torchconnector — pip install s3torchconnector · libregistry