Skip to content

[FEATURE] Add support for passing custom parameters to S3 put_object calls in S3SessionManager #1247

@itzimol

Description

@itzimol

Problem Statement

Currently, S3SessionManager doesn't allow users to specify additional S3 parameters like server-side encryption (SSE-KMS), encryption keys, or other configuration when storing session data. This limits the ability to meet security and compliance requirements when uploading the session objects to S3.

Proposed Solution

Add support for passing custom parameters to S3 put_object calls in S3SessionManager to enable encryption configuration and other S3-specific options. In particular, add a put_object_kwargs parameter to the S3SessionManager.__init__() method that accepts a dictionary of additional keyword arguments to pass through to all S3 put_object calls.

Example class initialization with new parameter (put_object_kwargs):

class S3SessionManager(RepositorySessionManager, SessionRepository):
    def __init__(
            self,
            session_id: str,
            bucket: str,
            prefix: str = "",
            boto_session: Optional[boto3.Session] = None,
            boto_client_config: Optional[BotocoreConfig] = None,
            region_name: Optional[str] = None,
            put_object_kwargs: Optional[Dict[str, Any]] = None,
            **kwargs: Any,
        ):

Updated function to upload session files to S3:

def _write_s3_object(self, key: str, data: Dict[str, Any]) -> None:
        """Write JSON object to S3."""
        try:
            content = json.dumps(data, indent=2, ensure_ascii=False)
            put_params = {
                "Bucket": self.bucket,
                "Key": key,
                "Body": content.encode("utf-8"),
                "ContentType": "application/json",
                **self.put_object_kwargs,
            }
            self.client.put_object(**put_params)
        except ClientError as e:
            raise SessionException(f"Failed to write S3 object {key}: {e}") from e

Use Case

Example usage:

from strands.session import S3SessionManager

session_manager = S3SessionManager(
    session_id="my-session",
    bucket="my-bucket",
    put_object_kwargs={
        "ServerSideEncryption": "aws:kms",
        "SSEKMSKeyId": "arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012"
    }
)

Alternatives Solutions

An alternative would be to have specific parameters for each S3 configuration instead of a "generic" put_object_kwargs, but this doesn't scale as the S3 PutObject API supports tens of parameters. This approach would:

  • Require adding dozens of individual parameters to __init__
  • Need constant maintenance as AWS adds new S3 features
  • Make the API verbose and harder to use
  • Not align with the "Extensible by design" tenet

The put_object_kwargs approach is more flexible, future-proof, and follows AWS SDK patterns.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions