AWS S3
rustac.store.S3Store
Interface to an Amazon S3 bucket.
All constructors will check for environment variables. All environment variables
starting with AWS_
will be evaluated. Names must match keys from
[S3ConfigInput
][obstore.store.S3ConfigInput]. Only upper-case environment
variables are accepted.
Some examples of variables extracted from environment:
AWS_ACCESS_KEY_ID
-> access_key_idAWS_SECRET_ACCESS_KEY
-> secret_access_keyAWS_DEFAULT_REGION
-> regionAWS_ENDPOINT
-> endpointAWS_SESSION_TOKEN
-> tokenAWS_CONTAINER_CREDENTIALS_RELATIVE_URI
-> https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.htmlAWS_REQUEST_PAYER
-> set to "true" to permit requester-pays connections.
Examples:
Using requester-pays buckets:
Pass request_payer=True
as a keyword argument or have AWS_REQUESTER_PAYS=True
set in the environment.
Anonymous requests:
Pass skip_signature=True
as a keyword argument or have AWS_SKIP_SIGNATURE=True
set in the environment.
__init__
__init__(
bucket: str | None = None,
*,
prefix: str | None = None,
config: S3Config | S3ConfigInput | None = None,
client_options: ClientConfig | None = None,
retry_config: RetryConfig | None = None,
credential_provider: S3CredentialProvider | None = None,
**kwargs: Unpack[S3ConfigInput],
) -> None
Create a new S3Store.
Parameters:
-
bucket
(str | None
, default:None
) –The AWS bucket to use.
Other Parameters:
-
prefix
(str | None
) –A prefix within the bucket to use for all operations.
-
config
(S3Config | S3ConfigInput | None
) –AWS configuration. Values in this config will override values inferred from the environment. Defaults to None.
-
client_options
(ClientConfig | None
) –HTTP Client options. Defaults to None.
-
retry_config
(RetryConfig | None
) –Retry configuration. Defaults to None.
-
credential_provider
(S3CredentialProvider | None
) –A callback to provide custom S3 credentials.
-
kwargs
(Unpack[S3ConfigInput]
) –AWS configuration values. Supports the same values as
config
, but as named keyword args.
Returns:
-
None
–S3Store
from_url
classmethod
from_url(
url: str,
*,
config: S3Config | S3ConfigInput | None = None,
client_options: ClientConfig | None = None,
retry_config: RetryConfig | None = None,
credential_provider: S3CredentialProvider | None = None,
**kwargs: Unpack[S3ConfigInput],
) -> S3Store
Parse available connection info from a well-known storage URL.
The supported url schemes are:
s3://<bucket>/<path>
s3a://<bucket>/<path>
https://s3.<region>.amazonaws.com/<bucket>
https://<bucket>.s3.<region>.amazonaws.com
https://ACCOUNT_ID.r2.cloudflarestorage.com/bucket
Parameters:
-
url
(str
) –well-known storage URL.
Other Parameters:
-
config
(S3Config | S3ConfigInput | None
) –AWS Configuration. Values in this config will override values inferred from the url. Defaults to None.
-
client_options
(ClientConfig | None
) –HTTP Client options. Defaults to None.
-
retry_config
(RetryConfig | None
) –Retry configuration. Defaults to None.
-
credential_provider
(S3CredentialProvider | None
) –A callback to provide custom S3 credentials.
-
kwargs
(Unpack[S3ConfigInput]
) –AWS configuration values. Supports the same values as
config
, but as named keyword args.
Returns:
-
S3Store
–S3Store
rustac.store.S3Config
Bases: TypedDict
Configuration parameters returned from S3Store.config.
Note that this is a strict subset of the keys allowed for input into the store, see [S3ConfigInput][obstore.store.S3ConfigInput].
aws_checksum_algorithm
instance-attribute
Sets the checksum algorithm which has to be used for object integrity check during upload.
aws_conditional_put
instance-attribute
See [S3ConfigInput.aws_conditional_put
][obstore.store.S3ConfigInput.aws_conditional_put].
aws_container_credentials_relative_uri
instance-attribute
See [S3ConfigInput.aws_container_credentials_relative_uri
][obstore.store.S3ConfigInput.aws_container_credentials_relative_uri].
aws_copy_if_not_exists
instance-attribute
See [S3ConfigInput.aws_copy_if_not_exists
][obstore.store.S3ConfigInput.aws_copy_if_not_exists].
aws_disable_tagging
instance-attribute
Disable tagging objects. This can be desirable if not supported by the backing store.
aws_endpoint
instance-attribute
Sets custom endpoint for communicating with AWS S3.
aws_metadata_endpoint
instance-attribute
Set the instance metadata endpoint
aws_request_payer
instance-attribute
If True
, enable operations on requester-pays buckets.
aws_server_side_encryption
instance-attribute
See [S3ConfigInput.aws_server_side_encryption
][obstore.store.S3ConfigInput.aws_server_side_encryption].
aws_session_token
instance-attribute
Token to use for requests (passed to underlying provider)
aws_skip_signature
instance-attribute
If True
, S3Store will not fetch credentials and will not sign requests.
aws_sse_bucket_key_enabled
instance-attribute
If set to True
, will use the bucket's default KMS key for server-side encryption.
If set to False
, will disable the use of the bucket's default KMS key for server-side encryption.
aws_sse_customer_key_base64
instance-attribute
The base64 encoded, 256-bit customer encryption key to use for server-side
encryption. If set, the server side encryption config value must be "sse-c"
.
aws_sse_kms_key_id
instance-attribute
The KMS key ID to use for server-side encryption.
If set, the server side encryption config value must be "aws:kms"
or "aws:kms:dsse"
.
aws_token
instance-attribute
Token to use for requests (passed to underlying provider)
aws_unsigned_payload
instance-attribute
Avoid computing payload checksum when calculating signature.
rustac.store.S3Credential
Bases: TypedDict
An S3 credential.
expires_at
instance-attribute
Expiry datetime of credential. The datetime should have time zone set.
If None, the credential will never expire.
rustac.store.S3CredentialProvider
Bases: Protocol
A type hint for a synchronous or asynchronous callback to provide custom S3 credentials.
This should be passed into the credential_provider
parameter of S3Store
.
Examples:
Return static credentials that don't expire:
def get_credentials() -> S3Credential:
return {
"access_key_id": "...",
"secret_access_key": "...",
"token": None,
"expires_at": None,
}
Return static credentials that are valid for 5 minutes:
from datetime import datetime, timedelta, UTC
async def get_credentials() -> S3Credential:
return {
"access_key_id": "...",
"secret_access_key": "...",
"token": None,
"expires_at": datetime.now(UTC) + timedelta(minutes=5),
}
A class-based credential provider with state:
from __future__ import annotations
from typing import TYPE_CHECKING
import boto3
import botocore.credentials
if TYPE_CHECKING:
from obstore.store import S3Credential
class Boto3CredentialProvider:
credentials: botocore.credentials.Credentials
def __init__(self, session: boto3.session.Session) -> None:
credentials = session.get_credentials()
if credentials is None:
raise ValueError("Received None from session.get_credentials")
self.credentials = credentials
def __call__(self) -> S3Credential:
frozen_credentials = self.credentials.get_frozen_credentials()
return {
"access_key_id": frozen_credentials.access_key,
"secret_access_key": frozen_credentials.secret_key,
"token": frozen_credentials.token,
"expires_at": None,
}
__call__
staticmethod
__call__() -> S3Credential | Coroutine[Any, Any, S3Credential]
Return an S3Credential
.