Storage API

Create and configure S3 buckets and sync them with rclone.

Cloud storage management utilities.

class cng_datasets.storage.RcloneSync(config_path: str | None = None, dry_run: bool = False)[source]

Bases: object

Manage rclone synchronization of datasets.

Handles syncing between different S3-compatible storage providers.

__init__(config_path: str | None = None, dry_run: bool = False)[source]

Initialize rclone sync manager.

Parameters:
  • config_path – Path to rclone config file

  • dry_run – If True, only simulate sync operations

configure_remote(remote_name: str, provider: str, access_key: str, secret_key: str, endpoint: str | None = None, region: str | None = None)[source]

Configure a new rclone remote.

Parameters:
  • remote_name – Name for the remote

  • provider – Provider type (s3, aws, cloudflare, etc.)

  • access_key – Access key ID

  • secret_key – Secret access key

  • endpoint – Endpoint URL for S3-compatible services

  • region – AWS region

copy(source: str, destination: str, **kwargs) → CompletedProcess[source]

Copy files from source to destination (doesn’t delete from destination).

Parameters:
  • source – Source path

  • destination – Destination path

  • **kwargs – Additional arguments passed to sync

Returns:

Completed process result

sync(source: str, destination: str, filters: List[str] | None = None, flags: List[str] | None = None) → CompletedProcess[source]

Sync files from source to destination.

Parameters:
  • source – Source path (remote:bucket/path or local path)

  • destination – Destination path (remote:bucket/path or local path)

  • filters – List of filter patterns (–include, –exclude)

  • flags – Additional rclone flags

Returns:

Completed process result

class cng_datasets.storage.S3Manager(access_key: str | None = None, secret_key: str | None = None, endpoint_url: str | None = None, region: str = 'us-west-2')[source]

Bases: object

Manage S3 buckets for dataset storage.

Handles bucket creation, CORS configuration, and public access setup.

__init__(access_key: str | None = None, secret_key: str | None = None, endpoint_url: str | None = None, region: str = 'us-west-2')[source]

Initialize S3 manager.

Parameters:
  • access_key – AWS access key ID

  • secret_key – AWS secret access key

  • endpoint_url – S3-compatible endpoint URL (for non-AWS providers)

  • region – AWS region

property client

Get or create boto3 S3 client.

configure_cors(bucket_name: str, allowed_origins: List[str] | None = None, allowed_methods: List[str] | None = None)[source]

Configure CORS for a bucket.

Parameters:
  • bucket_name – Bucket name

  • allowed_origins – List of allowed origins (default: [“*”])

  • allowed_methods – List of allowed methods (default: [“GET”, “HEAD”])

create_bucket(bucket_name: str, public: bool = False) → str[source]

Create an S3 bucket.

Parameters:
  • bucket_name – Name for the bucket

  • public – Whether to configure for public read access

Returns:

Bucket name

set_public_read(bucket_name: str)[source]

Configure bucket for public read access.

Parameters:

bucket_name – Bucket name

cng_datasets.storage.configure_bucket_cors(bucket_name: str, endpoint_url: str | None = None, **kwargs)[source]

Convenience function to configure CORS for a bucket.

Parameters:
  • bucket_name – Bucket name

  • endpoint_url – S3-compatible endpoint URL

  • **kwargs – Additional arguments passed to S3Manager

cng_datasets.storage.create_public_bucket(bucket_name: str, endpoint_url: str | None = None, **kwargs) → str[source]

Create a publicly accessible S3 bucket.

Parameters:
  • bucket_name – Name for the bucket

  • endpoint_url – S3-compatible endpoint URL

  • **kwargs – Additional arguments passed to S3Manager

Returns:

Bucket name

cng_datasets.storage.create_public_bucket_rclone(bucket_name: str, remote: str = 'nrp', set_cors: bool = True) → bool

Create a public bucket with rclone and set it to public read access.

Parameters:
  • bucket_name – Name of the bucket to create

  • remote – Rclone remote name (default: nrp)

  • set_cors – Whether to set CORS headers (default: True)

Returns:

True if successful, False otherwise

cng_datasets.storage.setup_public_bucket(bucket_name: str, remote: str = 'nrp', endpoint: str | None = None, set_cors: bool = True, verbose: bool = True) → bool[source]

Create and configure a public S3 bucket with proper permissions.

This function: 1. Creates the bucket if it doesn’t exist 2. Sets public read policy (list bucket + get objects) 3. Configures CORS headers for browser access

Parameters:
  • bucket_name – Name of the bucket to create/configure

  • remote – Rclone remote name (default: nrp)

  • endpoint – S3 endpoint URL (defaults to AWS_PUBLIC_ENDPOINT env var)

  • set_cors – Whether to configure CORS headers (default: True)

  • verbose – Print detailed progress (default: True)

Returns:

True if successful, False otherwise

Example

>>> setup_public_bucket("my-data-bucket")
Creating bucket: my-data-bucket
✓ Bucket created/verified
✓ Public read access enabled
✓ CORS configuration set
True