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

Uploaded Python 3

File details

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

File metadata

  • Download URL: simplest_async_s3client-0.0.7.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.7.tar.gz
Algorithm Hash digest
SHA256 9e36102997e569ff6d1354087a5e05e18a9451494d7f24b17a63ea96255356f8
MD5 03cbefc7f7d47af1f5032595fafca3fa
BLAKE2b-256 e1ca6c09f8d2b5536d2d2e5c007740983813ad4ccc56cfd80a854704ecf464b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for simplest_async_s3client-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a83f0c9ce192a12790917e8653b7183bfe7833a7dae8a07f62ed11a31c0b6751
MD5 314bf777a6f0cccd11598e9de16ed6f3
BLAKE2b-256 70979ba2d2dbf6bd6c7510781b58148c09756ec9748911aab0ab28a8a2b89420

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