Skip to main content

The simple module for putting and getting object from Amazon S3 compatible endpoints

Project description

aiohttp-s3-client

PyPI - License Wheel Mypy PyPI PyPI Coverage Status tox

The simple module for putting and getting object from Amazon S3 compatible endpoints

Installation

pip install aiohttp-s3-client

Usage

from http import HTTPStatus

from aiohttp import ClientSession
from aiohttp_s3_client import S3Client


async with ClientSession(raise_for_status=True) as session:
    client = S3Client(
        url="http://s3-url",
        session=session,
        access_key_id="key-id",
        secret_access_key="hackme",
        region="us-east-1"
    )

    # Upload str object to bucket "bucket" and key "str"
    async with client.put("bucket/str", "hello, world") as resp:
        assert resp.status == HTTPStatus.OK

    # Upload bytes object to bucket "bucket" and key "bytes"
    async with await client.put("bucket/bytes", b"hello, world") as resp:
        assert resp.status == HTTPStatus.OK

    # Upload AsyncIterable to bucket "bucket" and key "iterable"
    async def gen():
        yield b'some bytes'

    async with client.put("bucket/file", gen()) as resp:
        assert resp.status == HTTPStatus.OK

    # Upload file to bucket "bucket" and key "file"
    async with client.put_file("bucket/file", "/path_to_file") as resp:
        assert resp.status == HTTPStatus.OK

    # Check object exists using bucket+key
    async with client.head("bucket/key") as resp:
        assert resp == HTTPStatus.OK

    # Get object by bucket+key
    async with client.get("bucket/key") as resp:
        data = await resp.read()

    # Delete object using bucket+key
    async with client.delete("bucket/key") as resp:
        assert resp == HTTPStatus.NO_CONTENT

    # List objects by prefix
    async for result in client.list_objects_v2("bucket/", prefix="prefix"):
        # Each result is a list of metadata objects representing an object
        # stored in the bucket.
        do_work(result)

Bucket may be specified as subdomain or in object name:

client = S3Client(url="http://bucket.your-s3-host", ...)
async with client.put("key", gen()) as resp:
    ...

client = S3Client(url="http://your-s3-host", ...)
async with client.put("bucket/key", gen()) as resp:
    ...

client = S3Client(url="http://your-s3-host/bucket", ...)
async with client.put("key", gen()) as resp:
    ...

Auth may be specified with keywords or in URL:

client = S3Client(url="http://your-s3-host", access_key_id="key_id",
                  secret_access_key="access_key", ...)

client = S3Client(url="http://key_id:access_key@your-s3-host", ...)

Temporary credentials are supported by using a token.

client = S3Client(url="http://your-s3-host", access_key_id="key_id",
                  secret_access_key="access_key", session_token="token",
                  ...)

Multipart upload

For uploading large files multipart uploading can be used. It allows you to asynchronously upload multiple parts of a file to S3. S3Client handles retries of part uploads and calculates part hash for integrity checks.

client = S3Client()
await client.put_file_multipart(
    "test/bigfile.csv",
    headers={
    	"Content-Type": "text/csv",
    },
    workers_count=8,
)

Parallel download to file

S3 supports GET requests with Range header. It's possible to download objects in parallel with multiple connections for speedup. S3Client handles retries of partial requests and makes sure that file won't changed during download with ETag header. If your system supports pwrite syscall (linux, macos, etc) it will be used to write simultaneously to a single file. Otherwise, each worker will have own file which will be concatenated after downloading.

client = S3Client()
await client.get_file_parallel(
    "dump/bigfile.csv",
    "/home/user/bigfile.csv",
    workers_count=8,
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp_s3_client-0.8.11.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

aiohttp_s3_client-0.8.11-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_s3_client-0.8.11.tar.gz.

File metadata

  • Download URL: aiohttp_s3_client-0.8.11.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1040-azure

File hashes

Hashes for aiohttp_s3_client-0.8.11.tar.gz
Algorithm Hash digest
SHA256 46551292955b0da4d0731c2123d4ffe8d04ca3552c12c40932cd7a2113259d3e
MD5 b6ed92521cbb5628d84236813ecac087
BLAKE2b-256 b2103fbf60246d404d594bf4c226c86837121d527e8db460c39da350374ca684

See more details on using hashes here.

File details

Details for the file aiohttp_s3_client-0.8.11-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_s3_client-0.8.11-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1040-azure

File hashes

Hashes for aiohttp_s3_client-0.8.11-py3-none-any.whl
Algorithm Hash digest
SHA256 189f7376ef49b42d30892e7e8b65b9eefdab125c49a4c7d889d52f16d644733f
MD5 cfcc8c8d9fefc54850b191ced9ee48ce
BLAKE2b-256 e2b6dc98ad9371f8ff9fd0092169f12075dfb8f731c72f5df5c9f642796ab3ba

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page