---
sidebar:
  label: roboto.storage.upload_transaction
  order: 8
title: roboto.storage.upload_transaction
---
## Module Contents

### TransactionFile

```python
class roboto.storage.upload_transaction.TransactionFile
```

`from roboto.storage.upload_transaction import TransactionFile`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/upload_transaction.py#L40-L53)

Bases: `TypedDict`

A file to upload: the local file to read and the path to upload it to.

Items of the `items` argument of `UploadTransaction`.

**Attributes**

- **TransactionFile.destination_path** (`str`): Path within the association to upload the file to, such as `logs/run.mcap`.
- **TransactionFile.file_size** (`int`): Size of the file in bytes.
- **TransactionFile.local_path** (`pathlib.Path`): Local path of the file to upload.

### UploadTransaction

```python
class roboto.storage.upload_transaction.UploadTransaction(
    items: collections.abc.Sequence[TransactionFile],
    association: roboto.association.Association,
    device_id: Optional[str] = None,
    origination: Optional[str] = None,
    batch_size: Optional[int] = None,
    roboto_client: Optional[roboto.http.RobotoClient] = None,
    caller_org_id: Optional[str] = None,
)
```

`from roboto.storage.upload_transaction import UploadTransaction`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/upload_transaction.py#L72-L325)

An upload of one or more files to a dataset, device, device type, organization, or topic.

The SDK's own code uses it; to upload files, use [`upload_files()`](/docs/reference/python-sdk/roboto/domain/datasets/dataset#roboto.domain.datasets.dataset.Dataset.upload_files) or [`upload()`](/docs/reference/python-sdk/roboto/storage/file_service#roboto.storage.file_service.FileService.upload) instead. To upload with one:

1. Enter the transaction as a context manager, which begins the upload (see [`BeginUploadRequest`](/docs/reference/python-sdk/roboto/storage/api_operations#roboto.storage.api_operations.BeginUploadRequest)) and fetches the URI to write each file's bytes to.
2. Build an object store from the provider `make_credential_provider` returns.
3. Inside the store's `with` block, iterate over the transaction, start each yielded file's upload, and pass the future the store returns to `register_upload`. After each batch of `batch_size` files, the iteration waits for the batch's uploads with `await_uploads`, which reports the finished ones to Roboto.
4. Leave the transaction's `with` block. Leaving it without an exception completes the upload, which marks every file not reported yet [`Available`](/docs/reference/python-sdk/roboto/domain/files/record#roboto.domain.files.record.FileStatus.Available). Leaving it with an exception, such as the `ExceptionGroup` a failed upload raises, does not complete the upload, so a file whose bytes may be missing is never marked available.

Leaving the `with` block with registered uploads that were never waited for logs a warning.

**Parameters**

- **items** (`collections.abc.Sequence[TransactionFile]`): Files to upload, which iterating over the transaction yields.
- **association** (`roboto.association.Association`): Resource to upload the files to: a dataset, device, device type, organization, or topic.
- **device_id** (`Optional[str]`): ID of the device that produced the files. `None` records the calling device when a device makes the request, and no device otherwise.
- **origination** (`Optional[str]`): What uploaded the files, such as `roboto client v1.0.0`. `None` records the `ROBOTO_ENV` environment variable when it is set, and `roboto <version>` with the SDK's version otherwise.
- **batch_size** (`Optional[int]`): Number of files iteration yields before it waits for their uploads and reports them to Roboto. `None` puts every file in one batch.
- **roboto_client** (`Optional[roboto.http.RobotoClient]`): Client to make requests to Roboto with. Defaults to one configured from the environment, by [`from_env()`](/docs/reference/python-sdk/roboto/http/roboto_client#roboto.http.roboto_client.RobotoClient.from_env).
- **caller_org_id** (`Optional[str]`): Organization to act in. Required when the caller belongs to more than one organization; otherwise the caller's only organization.

**Properties**

- **UploadTransaction.completed_uploads** (`dict[pathlib.Path, str]`): Mapping from each local path whose upload Roboto marked available to the ID of the file it was uploaded to.

  Holds every upload `await_uploads` has reported so far. Iterating over the transaction reports each batch's successful uploads at the end of the batch.

- **UploadTransaction.transaction_id** (`str`): Unique ID of the upload, beginning with `tx_`, which is the `{upload_id}` in the routes that continue it.

  Read from [`transaction_id`](/docs/reference/python-sdk/roboto/storage/api_operations#roboto.storage.api_operations.BeginUploadResponse.transaction_id). Raises `Exception` until the transaction is entered as a context manager.

- **UploadTransaction.upload_mappings** (`dict[str, str]`): URI to write each file's bytes to, such as `s3://<bucket>/<key>`, keyed by the file's path.

  Read from [`upload_mappings`](/docs/reference/python-sdk/roboto/storage/api_operations#roboto.storage.api_operations.BeginUploadResponse.upload_mappings). Raises `Exception` until the transaction is entered as a context manager.

#### UploadTransaction.await_uploads()

```python
def await_uploads() -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/upload_transaction.py#L226-L259)

Wait for every registered upload to finish, then report the successful ones to Roboto.

Roboto marks each reported file [`Available`](/docs/reference/python-sdk/roboto/domain/files/record#roboto.domain.files.record.FileStatus.Available), and `completed_uploads` records its ID. Iterating over the transaction calls this at the end of each batch. Call it before the object store's `with` block exits, since the store may stop its transfers on exit.

**Raises**

- `ExceptionGroup`: One or more registered uploads failed. Raised after the successful uploads are reported, and holds each failed upload's exception.
- [`RobotoInternalException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoInternalException): Roboto's response to the report does not match the reported uploads one to one, so the uploaded paths cannot be matched to their files.

**Returns**

- `None`

#### UploadTransaction.make_credential_provider()

```python
def make_credential_provider() -> roboto.storage.object_store.CredentialProvider
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/upload_transaction.py#L261-L286)

Return a callable that fetches credentials that can write this upload's files.

Each call fetches fresh credentials from Roboto, so an object store can call it again to replace credentials that expire during a long upload.

**Returns**

- `roboto.storage.object_store.CredentialProvider`: Credential provider for an object store.

**Raises**

- [`RobotoInternalException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoInternalException): Roboto issues no credentials for the upload. Raised when the returned function is called.

#### UploadTransaction.register_upload()

```python
def register_upload(
    file: UploadableFile,
    future: roboto.storage.object_store.FutureLike[None],
) -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/upload_transaction.py#L288-L295)

Record a started upload, so that the next wait for uploads includes it.

**Parameters**

- **file** (`UploadableFile`): File the upload writes, as iterating over the transaction yielded it.
- **future** (`roboto.storage.object_store.FutureLike[None]`): Future the object store returned when the upload started.

**Returns**

- `None`

### UploadableFile

```python
class roboto.storage.upload_transaction.UploadableFile
```

`from roboto.storage.upload_transaction import UploadableFile`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/upload_transaction.py#L56-L69)

Bases: `TypedDict`

A file to upload, with the URI to write its bytes to.

Yielded by iterating over an `UploadTransaction`.

**Attributes**

- **UploadableFile.destination_path** (`str`): Path within the association to upload the file to, such as `logs/run.mcap`.
- **UploadableFile.local_path** (`pathlib.Path`): Local path of the file to upload.
- **UploadableFile.upload_uri** (`str`): URI to write the file's bytes to, from [`upload_mappings`](/docs/reference/python-sdk/roboto/storage/api_operations#roboto.storage.api_operations.BeginUploadResponse.upload_mappings).

### logger

```python
roboto.storage.upload_transaction.logger
```

`from roboto.storage.upload_transaction import logger`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/upload_transaction.py#L37-L37)
