rw

Read and write related API.

class s3pathlib.core.rw.ReadAndWriteAPIMixin[source]

A mixin class that implements the Text / Bytes, Read / Write methods.

read_bytes(version_id: str = OPT, if_match: str = OPT, if_modified_since: datetime = OPT, if_none_match: str = OPT, if_unmodified_since: datetime = OPT, range: str = OPT, response_cache_control: str = OPT, response_content_disposition: str = OPT, response_content_encoding: str = OPT, response_content_language: str = OPT, response_content_type: str = OPT, response_expires: str = OPT, sse_customer_algorithm: str = OPT, sse_customer_key: str = OPT, request_payer: str = OPT, part_number: int = OPT, expected_bucket_owner: str = OPT, checksum_mode: str = OPT, bsm: BotoSesManager | S3Client | None = None) bytes[source]

Read binary data from s3 object. A simple wrapper around get_object.

It also updates this S3Path object’s metadata and attributes like etag, size, version_id, etc with the get_object response.

Example:

>>> s3path = S3Path.from_s3_uri("s3://my-bucket/my-file.txt")
>>> s3path.write_bytes(b"hello", metadata={"creator": "me"})
>>> s3path.read_bytes()
b'hello'
>>> s3path.size
5
>>> s3path.metadata
{'creator': 'me'}
Parameters:
Returns:

the binary data.

Added in version 1.0.3.

Changed in version 1.1.2: automatically store metadata in cache.

Changed in version 2.0.1: add version_id parameter, and now support full list of get_object arguments.

read_text(encoding='utf-8', errors='strict', version_id: str = OPT, if_match: str = OPT, if_modified_since: datetime = OPT, if_none_match: str = OPT, if_unmodified_since: datetime = OPT, range: str = OPT, response_cache_control: str = OPT, response_content_disposition: str = OPT, response_content_encoding: str = OPT, response_content_language: str = OPT, response_content_type: str = OPT, response_expires: str = OPT, sse_customer_algorithm: str = OPT, sse_customer_key: str = OPT, request_payer: str = OPT, part_number: int = OPT, expected_bucket_owner: str = OPT, checksum_mode: str = OPT, bsm: BotoSesManager | S3Client | None = None) str[source]

Read text data from s3 object. A simple wrapper around get_object.

It also updates this S3Path object’s metadata and attributes like etag, size, version_id, etc with the get_object response.

Example:

>>> s3path = S3Path("s3://my-bucket/my-file.txt")
>>> s3path.write_text("hello", metadata={"creator": "me"})
>>> s3path.read_text()
'hello'
>>> s3path.size
5
>>> s3path.metadata
{'creator': 'me'}
Parameters:
Returns:

the string data.

Added in version 1.0.3.

Changed in version 1.1.2: automatically store metadata in cache.

Changed in version 2.0.1: add version_id parameter, and now support full list of get_object arguments.

write_bytes(data: bytes, metadata: Dict[str, str] = OPT, tags: Dict[str, str] = OPT, acl: str = OPT, cache_control: str = OPT, content_disposition: str = OPT, content_encoding: str = OPT, content_language: str = OPT, content_length: int = OPT, content_md5: str = OPT, content_type: str = OPT, checksum_algorithm: str = OPT, checksum_crc32: str = OPT, checksum_crc32c: str = OPT, checksum_sha1: str = OPT, checksum_sha256: str = OPT, expires_datetime: datetime = OPT, grant_full_control: str = OPT, grant_read: str = OPT, grant_read_acp: str = OPT, grant_write_acp: str = OPT, server_side_encryption: str = OPT, storage_class: str = OPT, website_redirect_location: str = OPT, sse_customer_algorithm: str = OPT, sse_customer_key: str = OPT, sse_kms_key_id: str = OPT, sse_kms_encryption_context: str = OPT, bucket_key_enabled: bool = OPT, request_payer: str = OPT, object_lock_mode: str = OPT, object_lock_retain_until_datetime: datetime = OPT, object_lock_legal_hold_status: str = OPT, expected_bucket_owner: str = OPT, bsm: BotoSesManager | S3Client | None = None) S3Path[source]

Write binary data to s3 object. A simple wrapper around put_object.

Example:

>>> s3path = S3Path("s3://my-bucket/my-file.txt")
>>> s3path.write_bytes(b"hello", metadata={"creator": "me"})
>>> s3path.size
5
>>> s3path.metadata
{'creator': 'me'}
Parameters:
Returns:

A new S3Path object with the same bucket and key, but the new metadata representing the object you just put.

Added in version 1.0.3.

Changed in version 1.1.1: add metadata and tags parameters.

write_text(data: str, encoding: str = 'utf-8', errors: str = 'strict', metadata: Dict[str, str] = OPT, tags: Dict[str, str] = OPT, acl: str = OPT, cache_control: str = OPT, content_disposition: str = OPT, content_encoding: str = OPT, content_language: str = OPT, content_length: int = OPT, content_md5: str = OPT, content_type: str = OPT, checksum_algorithm: str = OPT, checksum_crc32: str = OPT, checksum_crc32c: str = OPT, checksum_sha1: str = OPT, checksum_sha256: str = OPT, expires_datetime: datetime = OPT, grant_full_control: str = OPT, grant_read: str = OPT, grant_read_acp: str = OPT, grant_write_acp: str = OPT, server_side_encryption: str = OPT, storage_class: str = OPT, website_redirect_location: str = OPT, sse_customer_algorithm: str = OPT, sse_customer_key: str = OPT, sse_kms_key_id: str = OPT, sse_kms_encryption_context: str = OPT, bucket_key_enabled: bool = OPT, request_payer: str = OPT, object_lock_mode: str = OPT, object_lock_retain_until_datetime: datetime = OPT, object_lock_legal_hold_status: str = OPT, expected_bucket_owner: str = OPT, bsm: BotoSesManager | S3Client | None = None) S3Path[source]

Write text to s3 object. A simple wrapper around put_object.

Example:

>>> s3path = S3Path("s3://my-bucket/my-file.txt")
>>> s3path.write_text("hello", metadata={"creator": "me"})
>>> s3path.size
5
>>> s3path.metadata
{'creator': 'me'}
Parameters:
Returns:

A new S3Path object with the same bucket and key, but the new metadata representing the object you just put.

Added in version 1.0.3.

Changed in version 1.1.1: add metadata and tags parameters.

touch(exist_ok: bool = False, metadata: Dict[str, str] = OPT, tags: Dict[str, str] = OPT, acl: str = OPT, cache_control: str = OPT, content_disposition: str = OPT, content_encoding: str = OPT, content_language: str = OPT, content_length: int = OPT, content_md5: str = OPT, content_type: str = OPT, checksum_algorithm: str = OPT, checksum_crc32: str = OPT, checksum_crc32c: str = OPT, checksum_sha1: str = OPT, checksum_sha256: str = OPT, expires_datetime: datetime = OPT, grant_full_control: str = OPT, grant_read: str = OPT, grant_read_acp: str = OPT, grant_write_acp: str = OPT, server_side_encryption: str = OPT, storage_class: str = OPT, website_redirect_location: str = OPT, sse_customer_algorithm: str = OPT, sse_customer_key: str = OPT, sse_kms_key_id: str = OPT, sse_kms_encryption_context: str = OPT, bucket_key_enabled: bool = OPT, request_payer: str = OPT, object_lock_mode: str = OPT, object_lock_retain_until_datetime: datetime = OPT, object_lock_legal_hold_status: str = OPT, expected_bucket_owner: str = OPT, bsm: BotoSesManager | S3Client | None = None)[source]

Create an empty S3 object at the S3 location if the S3 object not exists. Do nothing if already exists.

Example:

>>> s3path = S3Path("s3://my-bucket/my-file.txt")
>>> s3path.write_text("hello", metadata={"creator": "me"})
>>> s3path.size
5
>>> s3path.metadata
{'creator': 'me'}
Parameters:

Added in version 1.0.6.

Changed in version 1.2.1: add metadata and tags parameters.

mkdir(exist_ok: bool = False, parents: bool = False, bsm: BotoSesManager | S3Client | None = None)[source]

Make an S3 folder (empty “/” file)

Example:

>>> s3dir = S3Path("s3://my-bucket/my-folder/").to_dir()
>>> s3dir.to_dir(exist_ok=True)
Parameters:
  • exist_ok – If True, it won’t raise error when the S3 folder already exists.

  • parents – If True, all parent folders will be created.

  • bsm – See bsm.

Added in version 1.0.6.