-
Notifications
You must be signed in to change notification settings - Fork 168
Experimental: AWS S3 storage driver #1388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jmaeagle99
wants to merge
16
commits into
temporalio:main
Choose a base branch
from
jmaeagle99:s3-driver-extra
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,635
−29
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
66a3fea
Experimental: AWS S3 driver extra package
jmaeagle99 2cdf63f
Remove unrefrenced readme
jmaeagle99 3ef7338
Mark module experimental
jmaeagle99 ae44795
Change driver name default and escape key segments
jmaeagle99 5667db3
Only allow sha256 hashes
jmaeagle99 1cb1235
Cancel in-flight operations when one fails
jmaeagle99 d612fa7
Return S3StorageDriverClient
jmaeagle99 4cd94ab
Update samples
jmaeagle99 e3e4295
Consolidate test files
jmaeagle99 f84c7d3
Format
jmaeagle99 5dfc3ee
Merge branch 'main' into s3-driver-extra
jmaeagle99 e00cb31
Better error message checking
jmaeagle99 b2726c8
Move aioboto3 impls to separate module
jmaeagle99 5df6179
Qualify type names
jmaeagle99 fccd877
Leave namespace casing as-is
jmaeagle99 5d93f46
Comment update
jmaeagle99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # AWS Integration for Temporal Python SDK | ||
|
|
||
| > ⚠️ **This package is currently at an experimental release stage.** ⚠️ | ||
|
|
||
| This package provides AWS integrations for the Temporal Python SDK, including an Amazon S3 driver for [external storage](../../../README.md#external-storage). | ||
|
|
||
| ## S3 Driver | ||
|
|
||
| `S3StorageDriver` stores and retrieves Temporal payloads in Amazon S3. It accepts any `S3StorageDriverClient` implementation and a `bucket` — either a static name or a callable for dynamic per-payload selection. | ||
|
|
||
| ### Using the built-in aioboto3 client | ||
|
|
||
| The SDK ships with an [`aioboto3`](https://github.com/terrycain/aioboto3)-based client. Install the extra to pull in its dependencies: | ||
|
|
||
| python -m pip install "temporalio[aioboto3]" | ||
|
|
||
| ```python | ||
| import aioboto3 | ||
| import dataclasses | ||
| from temporalio.client import Client | ||
| from temporalio.contrib.aws.s3driver import S3StorageDriver | ||
| from temporalio.contrib.aws.s3driver.aioboto3 import new_aioboto3_client | ||
| from temporalio.converter import DataConverter, ExternalStorage | ||
|
|
||
| session = aioboto3.Session() | ||
| # Credentials and region are resolved automatically from the standard AWS credential | ||
| # chain e.g. environment variables, ~/.aws/config, IAM instance profile, and so on. | ||
| async with session.client("s3") as s3_client: | ||
| driver = S3StorageDriver( | ||
| client=new_aioboto3_client(s3_client), | ||
| bucket="my-temporal-payloads", | ||
| ) | ||
|
|
||
| client = await Client.connect( | ||
| "localhost:7233", | ||
| data_converter=dataclasses.replace( | ||
| DataConverter.default, | ||
| external_storage=ExternalStorage(drivers=[driver]), | ||
| ), | ||
| ) | ||
| ``` | ||
|
|
||
| ### Custom S3 client implementations | ||
|
|
||
| To use a different S3 library, subclass `S3StorageDriverClient` and implement `put_object`, `get_object`, and `object_exists`. The ABC has no external dependencies, so no AWS packages are required to import it. | ||
|
|
||
| ```python | ||
| from temporalio.contrib.aws.s3driver import S3StorageDriverClient | ||
|
|
||
| class MyS3Client(S3StorageDriverClient): | ||
| async def put_object(self, *, bucket: str, key: str, data: bytes) -> None: ... | ||
| async def object_exists(self, *, bucket: str, key: str) -> bool: ... | ||
| async def get_object(self, *, bucket: str, key: str) -> bytes: ... | ||
|
|
||
| driver = S3StorageDriver(client=MyS3Client(), bucket="my-temporal-payloads") | ||
| ``` | ||
|
|
||
| ### Key structure | ||
|
|
||
| Payloads are stored under content-addressable keys derived from a SHA-256 hash of the serialized payload bytes, segmented by namespace and workflow/activity identifiers when serialization context is available, e.g.: | ||
|
|
||
| v0/ns/my-namespace/wfi/my-workflow-id/d/sha256/<hash> | ||
|
|
||
| ### Notes | ||
|
|
||
| * Any driver used to store payloads must also be configured on the component that retrieves them. If the client stores workflow inputs using this driver, the worker must include it in its `ExternalStorage.drivers` list to retrieve them. | ||
| * The target S3 bucket must already exist; the driver will not create it. | ||
| * Identical serialized bytes within the same namespace and workflow (or activity) share the same S3 object — the key is content-addressable within that scope. The same bytes used across different workflows or namespaces produce distinct S3 objects because the key includes the namespace and workflow/activity identifiers. | ||
| * Only payloads at or above `ExternalStorage.payload_size_threshold` (default: 256 KiB) are offloaded; smaller payloads are stored inline. Set `ExternalStorage.payload_size_threshold` to `None` to offload every payload regardless of size. | ||
| * `S3StorageDriver.max_payload_size` (default: 50 MiB) sets a hard upper limit on the serialized size of any single payload. A `ValueError` is raised at store time if a payload exceeds this limit. Increase it if your workflows produce payloads larger than 50 MiB. | ||
| * Override `S3StorageDriver.driver_name` only when registering multiple `S3StorageDriver` instances with distinct configurations under the same `ExternalStorage.drivers` list. | ||
|
|
||
| ### Dynamic Bucket Selection | ||
|
|
||
| To select the S3 bucket per payload, pass a callable as `bucket`: | ||
|
|
||
| ```python | ||
| from temporalio.contrib.aws.s3driver import S3StorageDriver | ||
| from temporalio.contrib.aws.s3driver.aioboto3 import new_aioboto3_client | ||
|
|
||
| driver = S3StorageDriver( | ||
| client=new_aioboto3_client(s3_client), | ||
| bucket=lambda context, payload: ( | ||
| "large-payloads" if payload.ByteSize() > 10 * 1024 * 1024 else "small-payloads" | ||
| ), | ||
| ) | ||
| ``` | ||
|
|
||
| ### Required IAM permissions | ||
|
|
||
| The AWS credentials used by your S3 client must have the following S3 permissions on the target bucket and its objects: | ||
|
|
||
| ```json | ||
| { | ||
| "Effect": "Allow", | ||
| "Action": [ | ||
| "s3:PutObject", | ||
| "s3:GetObject" | ||
| ], | ||
| "Resource": "arn:aws:s3:::my-temporal-payloads/*" | ||
| } | ||
| ``` | ||
|
|
||
| `s3:PutObject` is required by components that store payloads (typically the Temporal client and worker sending workflow/activity inputs), and `s3:GetObject` is required by components that retrieve them (typically workers and clients reading results). Components that only retrieve payloads do not need `s3:PutObject`, and vice versa. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| """Amazon S3 storage driver for Temporal external storage. | ||
|
|
||
| .. warning:: | ||
| This API is experimental. | ||
| """ | ||
|
|
||
| from temporalio.contrib.aws.s3driver._client import S3StorageDriverClient | ||
| from temporalio.contrib.aws.s3driver._driver import S3StorageDriver | ||
|
|
||
| __all__ = [ | ||
| "S3StorageDriverClient", | ||
| "S3StorageDriver", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """S3 storage driver client abstraction for the S3 storage driver. | ||
|
|
||
| .. warning:: | ||
| This API is experimental. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
|
|
||
|
|
||
| class S3StorageDriverClient(ABC): | ||
jmaeagle99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Abstract base class for S3 object operations. | ||
|
|
||
| Implementations must support ``put_object`` and ``get_object``. Multipart | ||
| upload handling (if needed) is an internal concern of each implementation. | ||
|
|
||
| .. warning:: | ||
| This API is experimental. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| async def put_object(self, *, bucket: str, key: str, data: bytes) -> None: | ||
| """Upload *data* to the given S3 *bucket* and *key*.""" | ||
|
|
||
| @abstractmethod | ||
| async def object_exists(self, *, bucket: str, key: str) -> bool: | ||
| """Return ``True`` if an object exists at the given *bucket* and *key*.""" | ||
|
|
||
| @abstractmethod | ||
| async def get_object(self, *, bucket: str, key: str) -> bytes: | ||
| """Download and return the bytes stored at the given S3 *bucket* and *key*.""" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.