Cloud File Storage#

Cybsi Cloud provides the API to store files. The files could be used for enrichment, etc.

The easiest way to upload a file is using the upload method.

#!/usr/bin/env python3
import os

from cybsi.cloud import Client, Config

if __name__ == "__main__":
    file_path = "/tmp/file"
    size = os.path.getsize(file_path)

    config = Config(api_key="the cryptic string")
    with Client(config) as client:
        with open(file_path, "rb") as f:
            ref = client.files.upload(f, name=f.name, size=size)
            print(ref.id)

Also there is a low-level API to upload files by parts. It can be used to implement parallel file uploading.

#!/usr/bin/env python3
import os

from cybsi.cloud import Client, Config
from cybsi.cloud.files import LimitedReader

if __name__ == "__main__":
    part_size = 10 * (1 << 20)  # 10mb
    file_path = "/tmp/file"
    file_size = os.path.getsize(file_path)

    def iter_part_size():
        rest = file_size
        while rest > 0:
            size = rest if rest < part_size else part_size
            yield size
            rest -= size

    config = Config(api_key="the cryptic string")
    with Client(config) as client:
        session = client.files.create_session(part_size=part_size)
        with open(file_path, "rb") as f:
            for part_number, part_size in enumerate(iter_part_size(), start=1):
                client.files.upload_session_part(
                    LimitedReader(f, limit=part_size),
                    session_id=session.id,
                    part_number=part_number,
                    size=part_size,
                )
        ref = client.files.complete_session(session.id)
        print(ref.id)

To download the file use the download method:

#!/usr/bin/env python3
import uuid

from cybsi.cloud import Client, Config

if __name__ == "__main__":
    config = Config(api_key="the cryptic string")
    file_id = uuid.UUID("5d63fd9a-4ffb-46e7-afa1-cfbb0227ad3d")
    buf_size = 4096

    with Client(config) as client:
        with open("/tmp/out.dat", "wb") as f:
            with client.files.download(file_id) as content:
                buf = content.read(buf_size)
                while buf:
                    f.write(buf)
                    buf = content.read(buf_size)