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

install

pip install simplest_async_s3client

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: simplest_async_s3client-0.0.6.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.6.tar.gz
Algorithm Hash digest
SHA256 9f536463352c091f9c00f3054ed52cffcab5cf442a070d320be8a297ae31a7e9
MD5 37852c572cd4a6c987d6186d15e27e8f
BLAKE2b-256 07caf961f6ea979ca6a11a7e6b7df5324a30e5ee1091a2db72db751f30d9d738

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for simplest_async_s3client-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ba828ed5f3dcbfe5bda405eedb694dc97286d7dcb97e85d8909631c9e23e0249
MD5 9cafe61677f3deb89e603280c556a77b
BLAKE2b-256 cda9a57d0b85410417deb7f1c37d252f38a1491f5acc110148dd138bba08024c

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