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 simple_async_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.4.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.4-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simplest_async_s3client-0.0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 c7322364b3b13d61a597a225ef9cb53a736f3d23b5e98d3d86f4af7be3fefa22
MD5 8b0a066103abde0fe86270176305973f
BLAKE2b-256 b91cd2dcc452e43e8fc9bab13eb2895c4181b97e3a030385dfce5d8d380a9247

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for simplest_async_s3client-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5b621a2def9bfeb22991c97c6224bc47ce39dd8b208a45baed47ab8343708bec
MD5 eda662c52fe4470ddc64bc33fefae654
BLAKE2b-256 8c4e53c64adf0cf52411dd7d32e16c1ca2146536978e98dde9a985bdf2e065e7

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