> ## Documentation Index
> Fetch the complete documentation index at: https://docs.baseten.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Download data in your training script

> Download training data from Amazon S3, Hugging Face, or Google Cloud Storage inside your training code, with credentials stored as Baseten secrets.

Download data in your training script when you need a library's own loader, such as `datasets.load_dataset`, or custom download logic. For read-only files in an `hf://`, `s3://`, `gs://`, `r2://`, or `cw://` source, [mount them with BDN](https://docs.baseten.co/training/load-with-bdn.md) instead.

Each tab needs the source's client library in the job's image: `boto3` for Amazon S3, `datasets` for Hugging Face, and `google-cloud-storage` for Google Cloud Storage. Use a [`base_image`](https://docs.baseten.co/reference/sdk/training#param-base-image) that includes it, or install it in [`start_commands`](https://docs.baseten.co/reference/sdk/training#param-start-commands) before your code runs.

The snippets that set [`Runtime`](https://docs.baseten.co/reference/sdk/training#runtime) environment variables go in your job's `config.py`, alongside the rest of the job definition described in [the quickstart](https://docs.baseten.co/training/getting-started.md).

To read a bucket without storing a key, [mount it with BDN](https://docs.baseten.co/training/load-with-bdn.md#authentication) and OIDC instead.

> [tab] Amazon S3
>
> **To load data from Amazon S3**:
>
> 1. Save your AWS credentials as [Baseten secrets](https://docs.baseten.co/organization/secrets.md) named `aws_access_key_id` and `aws_secret_access_key`.
>
> 2. Add this to the job's `Runtime` in `config.py`:
>
>    ```python theme={"system"}
>    from truss_train import definitions
>
>    runtime = definitions.Runtime(
>        environment_variables={
>            "AWS_ACCESS_KEY_ID": definitions.SecretReference(name="aws_access_key_id"),
>            "AWS_SECRET_ACCESS_KEY": definitions.SecretReference(name="aws_secret_access_key"),
>        },
>    )
>    ```
>
> 3. Download from S3 in your training script:
>
>    ```python theme={"system"}
>    import os
>
>    import boto3
>
>    target = os.path.join(os.environ["BT_SCRATCH_DIR"], "training-data.tar.gz")
>    s3 = boto3.client("s3")
>    s3.download_file("my-bucket", "training-data.tar.gz", target)
>    print("downloaded", os.path.getsize(target), "bytes")
>    ```
>
>    The script prints `downloaded <bytes> bytes`, where `<bytes>` is the size of the downloaded file.
>
> > [!TIP]
> >
> > To avoid re-downloading large datasets on each job, download to the [training cache](https://docs.baseten.co/training/concepts/cache.md) instead, and skip the download when the file is already there:
> >
> > ```python theme={"system"}
> > import os
> >
> > import boto3
> >
> > target = os.path.join(os.environ["BT_PROJECT_CACHE_DIR"], "training-data.tar.gz")
> > if not os.path.exists(target):
> >     boto3.client("s3").download_file("my-bucket", "training-data.tar.gz", target)
> > ```

> [tab] Hugging Face
>
> **To load data from Hugging Face**:
>
> 1. For a private or gated dataset, save a Hugging Face access token as a [Baseten secret](https://docs.baseten.co/organization/secrets.md) named `hf_access_token`. Public datasets need no token, so skip this step and the next one.
>
> 2. Add this to the job's `Runtime` in `config.py` to expose the token as `HF_TOKEN`:
>
>    ```python theme={"system"}
>    from truss_train import definitions
>
>    runtime = definitions.Runtime(
>        environment_variables={
>            "HF_TOKEN": definitions.SecretReference(name="hf_access_token"),
>        },
>    )
>    ```
>
> 3. Load the dataset in your training script. The `datasets` library reads `HF_TOKEN` from the environment:
>
>    ```python theme={"system"}
>    from datasets import load_dataset
>
>    ds = load_dataset("your-username/your-dataset", split="train")
>    print(len(ds), "rows")
>    ```
>
>    The script prints the number of rows in the dataset.

> [tab] Google Cloud Storage
>
> **To load data from Google Cloud Storage**:
>
> 1. Save a service account key JSON file with read access to your bucket as a [Baseten secret](https://docs.baseten.co/organization/secrets.md), for example `gcp_service_account_key`.
>
> 2. Add this to the job's `Runtime` in `config.py`:
>
>    ```python theme={"system"}
>    from truss_train import definitions
>
>    runtime = definitions.Runtime(
>        environment_variables={
>            "GCP_SERVICE_ACCOUNT_KEY": definitions.SecretReference(name="gcp_service_account_key"),
>        },
>    )
>    ```
>
> 3. Download the file in your training script:
>
>    ```python theme={"system"}
>    import json
>    import os
>
>    from google.cloud import storage
>
>    client = storage.Client.from_service_account_info(
>        json.loads(os.environ["GCP_SERVICE_ACCOUNT_KEY"])
>    )
>    target = os.path.join(os.environ["BT_SCRATCH_DIR"], "training-data.tar.gz")
>    client.bucket("my-bucket").blob("training-data.tar.gz").download_to_filename(target)
>    print("downloaded", os.path.getsize(target), "bytes")
>    ```
>
>    The script prints `downloaded <bytes> bytes`, where `<bytes>` is the size of the downloaded file.

## Next steps

* [Training cache](https://docs.baseten.co/training/concepts/cache.md): Keep downloaded data between jobs so later jobs skip the download.
* [Load weights and data with BDN](https://docs.baseten.co/training/load-with-bdn.md): Mount read-only data before your code starts.
