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

# Object storage

> Colocate datasets with your compute and save training checkpoints between runs

Keep datasets and checkpoints in SF Compute (SFC) object storage, close to your compute. Buckets
hold your files, and tools built for S3-compatible object stores, such as rclone and boto3, read
and write to them. Upload data from your laptop or another storage provider, then use it on SFC
[instances](https://docs.sfcompute.com/preview/instances.md). Uploaded data stays after an instance terminates.

## Create a bucket and access key

Create a bucket for your data and an access key that your tools use to reach it.

Creating and deleting buckets and access keys in a workspace requires a [role](https://docs.sfcompute.com/preview/roles.md) that
allows the `bucket` and `access_key` resources there. The built-in admin, operator, and
workspace-admin roles allow both.

Creating buckets and access keys requires your organization to set up billing. Have a team member
with the [admin](https://docs.sfcompute.com/preview/roles.md#admin-role) or [billing](https://docs.sfcompute.com/preview/roles.md#billing-role) role complete
the following under [Billing](https://console.sfcompute.com/dashboard/billing) in the console:

* A billing profile
* A primary billing contact
* A card set as the default payment method

![Billing page with the Payment Methods, Billing Profile, and Billing Contacts sections, and a card marked Default](https://mintcdn.com/sfcompute/09S6weu1ec9DZnQI/images/object-storage/billing-setup.png?fit=max\&auto=format\&n=09S6weu1ec9DZnQI\&q=85\&s=916974246f6e32c79fad50a8b9cea9e2)

The examples use the `europe-north1`
[region](https://docs.sfcompute.com/api-reference/skus/get-sku.md#response-region-one-of-1) and your
active [workspace](https://docs.sfcompute.com/preview/workspaces.md). Replace `acme-training-data` throughout with your own
bucket name.

> [!NOTE]
>
> Object storage isn't available in every region. The console and CLI list supported regions when you
> create a bucket. If you need storage in a region we don't support yet,
> [contact us](https://sfcompute.com/contact).

> [tab] CLI
>
> [Install `sf` and log in](https://docs.sfcompute.com/preview/quick-start#install-the-cli), then create a bucket and key in
> the same region.
>
> ```bash theme={null}
> sf storage buckets create --name acme-training-data --region europe-north1
> sf storage access-key create --name training --region europe-north1
> ```
>
> Save the returned access key ID and secret in your secrets manager. `sf` shows the secret only
> once.

> [tab] Console
>
> **1. Open the create form**
>
> Open [Storage → Buckets](https://console.sfcompute.com/dashboard/storage?tab=buckets) and
> click **Create Bucket**. Enter a name and region, and choose a workspace if offered.
>
> ![Storage page on the Buckets tab, with the Create Bucket button at the top right](https://mintcdn.com/sfcompute/09S6weu1ec9DZnQI/images/object-storage/buckets-list.png?fit=max\&auto=format\&n=09S6weu1ec9DZnQI\&q=85\&s=d13d5fc69b1ef37c344064bfc7f6ada2)
>
> **2. Create the bucket**
>
> If the workspace has no access keys in that region yet, the form shows **Also create an
> access key**. Select it and click **Create Bucket & Key**. If the workspace already has a
> key there, click **Create Bucket** and reuse that key.
>
> ![Create bucket form for acme-training-data in europe-north1, with Also create an access key selected](https://mintcdn.com/sfcompute/09S6weu1ec9DZnQI/images/object-storage/create-bucket-form.png?fit=max\&auto=format\&n=09S6weu1ec9DZnQI\&q=85\&s=4df00868f4e56f1463cd12e83a692916)
>
> **3. Save the key**
>
> Save a new key's **Access key ID** and **Secret access key** in your secrets manager. The
> console shows the secret only once. To create another key, open [Access
> keys](https://console.sfcompute.com/dashboard/storage?tab=keys).
>
> ![Bucket created dialog showing an example access key ID and secret access key](https://mintcdn.com/sfcompute/09S6weu1ec9DZnQI/images/object-storage/bucket-created-key.png?fit=max\&auto=format\&n=09S6weu1ec9DZnQI\&q=85\&s=4c378c3b9bccb3e625f7d1b6acbd6f52)

You may reuse an existing access key if it's in the same workspace and region as the bucket.

> [!NOTE]
>
> An access key can read and write every bucket in its workspace and region. Use separate workspaces
> when you need separate access boundaries.

### Set your storage credentials

Set these variables on your laptop and on each instance that uses the bucket. Find the endpoint in
the bucket's **Access bucket** panel or with `sf storage buckets get acme-training-data`.

![Access bucket panel showing the rclone snippet with the bucket endpoint](https://mintcdn.com/sfcompute/09S6weu1ec9DZnQI/images/object-storage/access-bucket.png?fit=max\&auto=format\&n=09S6weu1ec9DZnQI\&q=85\&s=8523837985068d8c21bc0acbc4424445)

`sf storage access-key create --format env` and the console's **Access bucket** snippets print
`AWS_*` names instead. Rename them as shown so they don't override your existing AWS credentials.

```bash theme={null}
export SFC_ACCESS_KEY_ID="your_access_key_id"
export SFC_SECRET_ACCESS_KEY="your_secret_access_key"
export SFC_ENDPOINT_URL="https://object.europe-north1.sfcompute.com"
export SFC_REGION="europe-north1"
```

## Bring your dataset to SFC

Copy your dataset into the bucket with rclone, then copy it onto each instance that trains on it.
Run these commands on your laptop or the machine doing the transfer. Install
[rclone](https://rclone.org/install/), then configure an SFC remote with your
[storage credentials](#set-your-storage-credentials).

```bash theme={null}
rclone config create sfc s3 \
  provider=Other \
  access_key_id="$SFC_ACCESS_KEY_ID" \
  secret_access_key="$SFC_SECRET_ACCESS_KEY" \
  endpoint="$SFC_ENDPOINT_URL" region="$SFC_REGION" \
  force_path_style=true
```

Choose your source:

> [tab] From your laptop
>
> Copy your dataset into a versioned directory in the bucket.
>
> ```bash theme={null}
> rclone copy ./dataset sfc:acme-training-data/datasets/v1 --progress
> ```

> [tab] From another S3 provider
>
> Use `rclone config` to add a remote named `source` with that provider's endpoint and its own
> credentials. Then copy between the two remotes.
>
> ```bash theme={null}
> rclone copy source:source-bucket/dataset \
>   sfc:acme-training-data/datasets/v1 --progress
> ```
>
> Data passes through the machine running rclone. The transfer uses that machine's bandwidth, and
> your source provider may charge egress fees.

Rerun `copy` to transfer new or changed files. Use a new directory such as `datasets/v2` for a new
dataset version. To mirror the source exactly, use `rclone sync`; it also deletes destination files
absent from the source.

### Use the dataset on an instance

[Connect to your instance](https://docs.sfcompute.com/preview/instances.md#ssh-into-an-instance), set your
[storage credentials](#set-your-storage-credentials), and configure rclone as above. Copy the
dataset onto its local disk before starting training.

```bash theme={null}
mkdir -p ~/datasets/v1
rclone copy sfc:acme-training-data/datasets/v1 ~/datasets/v1 --progress
```

Point your training data loader at `~/datasets/v1`. Repeat on each instance that needs a local copy.

## Save and resume training checkpoints

From an SFC instance, save checkpoints to object storage in the same region as your training job.
Checkpoints in object storage stay available when you move to another instance or start another run.

With PyTorch already installed, run `python -m pip install boto3` in your training environment. Set
your [storage credentials](#set-your-storage-credentials) there. Add this to your training loop,
where `model`, `optimizer`, and `step` are your current training state:

```python theme={null}
import os
from pathlib import Path

import boto3
import torch
from botocore.config import Config

s3 = boto3.client(
    "s3",
    aws_access_key_id=os.environ["SFC_ACCESS_KEY_ID"],
    aws_secret_access_key=os.environ["SFC_SECRET_ACCESS_KEY"],
    endpoint_url=os.environ["SFC_ENDPOINT_URL"],
    region_name=os.environ["SFC_REGION"],
    config=Config(s3={"addressing_style": "path"}),
)

checkpoint = Path("checkpoints") / f"step-{step}.pt"
checkpoint.parent.mkdir(parents=True, exist_ok=True)
torch.save({
    "step": step,
    "model": model.state_dict(),
    "optimizer": optimizer.state_dict(),
}, checkpoint)
key = f"runs/run-42/step-{step}.pt"
s3.upload_file(str(checkpoint), "acme-training-data", key)
```

Use a different run directory for each job. Upload only completed checkpoints; for distributed
training, coordinate the writers so they don't overwrite each other. Wait for the upload to succeed
before terminating the instance.

On a fresh instance, recreate the model and optimizer, configure the S3 client as above, and restore
a completed checkpoint:

```python theme={null}
key = "runs/run-42/step-1000.pt"
s3.download_file("acme-training-data", key, "checkpoint.pt")
state = torch.load("checkpoint.pt", map_location="cpu", weights_only=True)
model.load_state_dict(state["model"])
optimizer.load_state_dict(state["optimizer"])
start_step = state["step"] + 1
```

Include any other state your job needs to resume, such as its learning-rate scheduler. See
[PyTorch checkpointing](https://docs.pytorch.org/tutorials/beginner/saving_loading_models.html#saving-loading-a-general-checkpoint-for-inference-and-or-resuming-training).

To retrieve a run on your own machine, use the same rclone remote:

```bash theme={null}
rclone copy sfc:acme-training-data/runs/run-42 ./run-42 --progress
```

## Manage buckets and access keys

List buckets and keys with `sf storage buckets list` and `sf storage access-key list`, or open
[Storage](https://console.sfcompute.com/dashboard/storage) in the console. Revoke a key with `sf
storage access-key delete training`, or open [Storage → Access
keys](https://console.sfcompute.com/dashboard/storage?tab=keys) and click **Revoke Key**.

![Access keys tab with the actions menu open for the training key, showing Revoke Key](https://mintcdn.com/sfcompute/09S6weu1ec9DZnQI/images/object-storage/access-keys-revoke.png?fit=max\&auto=format\&n=09S6weu1ec9DZnQI\&q=85\&s=621892233bfd68477e391b4070d845eb)

When you no longer need a bucket, run `sf storage buckets delete acme-training-data` or select
**Delete Bucket** in [Storage](https://console.sfcompute.com/dashboard/storage). Confirm the name
when prompted. Deletion removes the bucket and all its objects.

![Buckets tab with the actions menu open for acme-training-data, showing Delete Bucket](https://mintcdn.com/sfcompute/09S6weu1ec9DZnQI/images/object-storage/delete-bucket.png?fit=max\&auto=format\&n=09S6weu1ec9DZnQI\&q=85\&s=5874586e2413dae85c13bf07ff0655a5)

> [!WARNING]
>
> Deleting a bucket is permanent. Its name becomes available for anyone to claim.

## Bucket and access key limits

SF Compute enforces these limits on buckets and access keys:

| Resource | Limit |
| - | - |
| Bucket capacity | 100 TiB per bucket by default. [Contact us](https://sfcompute.com/contact) if you need a higher limit. |
| Access keys | 2 per workspace per region. Key names must be unique within a workspace. |
| Bucket names | Globally unique. 3 to 63 lowercase letters, numbers, or hyphens, starting with a letter or number. |

## Usage and billing

SF Compute measures the bytes stored in each bucket hourly. Storage costs \$0.14 per GiB-month,
prorated by amount stored and time kept. For example, 100 GiB for a full calendar month costs \$14,
and for half that month it costs \$7.

SF Compute invoices storage after each UTC calendar month and
charges your default card when the invoice is due, 7 days after sending it. Storage charges are
separate from prepaid compute credits.

Track usage and month-to-date cost under [Usage →
Storage](https://console.sfcompute.com/dashboard/usage?view=storage) in the console.

![Usage page on the Storage tab, with stored bytes per bucket by day and month-to-date cost per bucket](https://mintcdn.com/sfcompute/09S6weu1ec9DZnQI/images/object-storage/usage-storage.png?fit=max\&auto=format\&n=09S6weu1ec9DZnQI\&q=85\&s=05da40b4040e08cc90fe8cc0f6777d42)

View invoices on the [Billing](https://console.sfcompute.com/dashboard/billing) page.

![Invoices section of the Billing page, listing each billing month with its status, total, and due date](https://mintcdn.com/sfcompute/09S6weu1ec9DZnQI/images/object-storage/billing-invoices.png?fit=max\&auto=format\&n=09S6weu1ec9DZnQI\&q=85\&s=1707690b1ad6bbac4917cab688f47acd)

## API reference

The SF Compute API doesn't manage buckets or access keys yet. Use `sf storage` to script them, or
[contact us](https://sfcompute.com/contact) if you need programmatic access.
