Extension for boto3 to ease work with s3
pip install saritasa-s3-tools
or if you are using poetry
poetry add saritasa-s3-tools
To install all optional dependencies add [all]
S3Client
andAsyncS3Client
for integrations with s3 bucketsS3FileTypeConfig
for defining configuration parameters for direct upload to s3S3Key
for generating unique keys for s3 uploadpytest
plugin with fixtures forboto3
,S3Client
andAsyncS3Client
import saritasa_s3_tools
import pathlib
import xml.etree.ElementTree
s3_client = saritasa_s3_tools.S3Client(
boto3_client=boto3_client,
default_bucket=s3_bucket,
)
s3_params = s3_client.generate_params(
filename=pathlib.Path(__file__).name,
config=saritasa_s3_tools.S3FileTypeConfig.configs["files"],
content_type="application/x-python-code",
extra_metadata={
"test": "123",
},
)
with (
httpx.Client() as client,
pathlib.Path(__file__).open("rb") as upload_file,
):
upload_response = client.post(
url=s3_params.url,
data={
key: value
for key, value in s3_params.params.items()
if value is not None
},
files={"file": upload_file.read()},
)
parsed_response = xml.etree.ElementTree.fromstring( # noqa: S314
upload_response.content.decode(),
)
file_key = parsed_response[2].text
file_url = parsed_response[0].text