Skip to main content

A simple async client for aws s3 storage

Project description

Simple async s3 client for immers.cloud

Source code https://github.com/Aberezhnoy1980/MarketGenius/tree/main/ds_app/src/clients/s3_client

Getting started

Get the access key, secret key and url (https://api.immers.cloud:8080) from immers.cloud and create an instance of the client:

from simplest_async_s3client.s3client import S3Client


your_access_key = 'access_key'
your_secret_key = 'secret_key'
your_endpoint_url = 'endpoint_url'

s3_client = S3Client(
    access_key=your_access_key,
    secret_key=your_secret_key,
    endpoint_url=your_endpoint_url,
)

This client contains several methods for working with objects (files):

  • If necessary, you can create a new bucket in the storage:
# For execute the coroutine you can use asyncio.
import asyncio

new_bucket_name = 'new_bucket_name'
asyncio.run(
    await s3_client.create_bucket(new_bucket_name)
)
  • Uploading files. You can send one or more objects to the storage:
# For a single file
async def upload_file(file_path, bucket_name):
    await s3_client.upload_file(file_path, bucket_name)

file_path = '/path/to/your/file'
bucket_name = 'your bucket name'

asyncio.run(upload_file(file_path, bucket_name))

# For a group of files or a directory. For example, you have a directory with files
import os


async def upload_files(dir_path, bucket):
    files_list = os.listdir(dir_path)
    file_paths = [os.path.join(dir_path, file) for file in files_list]

    for file_path in file_paths:
        await s3_client.upload_file(file_path, bucket)


dir_path = '/path/to/your/directory'
bucket_name = "bucket_name"

asyncio.run(upload_files(dir_path, bucket_name))
  • Getting a list of objects in storage:
async def get_file_list(bucket):
    obj_list = await s3_client.get_object_list(bucket)

    for item in obj_list:
        print(item["Key"], item["Size"])

bucket_name = 'bucket name'

asyncio.run(get_file_list(bucket_name))
  • Getting object. The file will be created in the destination directory (use '.' for current):
async def get_file(bucket, file_name, destination):
    response = await s3_client.get_object(bucket, file_name, destination)
    print(response)

    
bucket_name = 'bucket name' 
file_name = 'file name' # can get from get_file_list()
destination = 'path/to/destination/directory'

asyncio.run(get_file(bucket_name, file_name, destination))
  • Deleting files:
async def delete_file(bucket, objects_to_delete):
    resp = await s3_client.delete_objects(bucket, objects_to_delete)
    print(resp['ResponseMetadata']['HTTPStatusCode'])


objects_to_delete = [{"Key": "file_name"}]
bucket_name = "bucket name"

asyncio.run(delete_file(bucket_name, objects_to_delete))


# delete more than one file
async def delete_all(bucket):
    objects = await s3_client.get_object_list(bucket)
    objects_to_delete = []

    for obj in objects:
        objects_to_delete.append({'Key': obj['Key']})

    resp = await s3_client.delete_objects(bucket, objects_to_delete)
    print(resp['ResponseMetadata']['HTTPStatusCode'])


bucket_name = 'bucket name'

asyncio.run(delete_all(bucket_name))

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

simplest_async_s3client-0.0.5.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simplest_async_s3client-0.0.5-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file simplest_async_s3client-0.0.5.tar.gz.

File metadata

  • Download URL: simplest_async_s3client-0.0.5.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for simplest_async_s3client-0.0.5.tar.gz
Algorithm Hash digest
SHA256 6fe95bbdaa3551d83818f85fefb7c64ac95eef2cc7583be50bd4d2e95a7cca66
MD5 e3f4a7d1e7a41598458b4c48f9b85b88
BLAKE2b-256 68299f65f01c989b593265e0a93d97e36688fa401c872ff6ade3db6a828bb3e3

See more details on using hashes here.

File details

Details for the file simplest_async_s3client-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for simplest_async_s3client-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 700e00ebea548390e55d129636d9be841776a321e17825c0204aa6301ba4e975
MD5 4981efab93c608f5bfdd316c5c0dce39
BLAKE2b-256 88af99981523839fb1c67844e1280e220f2b776cd845791569a099c81717d295

See more details on using hashes here.

Supported by

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