diff --git a/examples/store.py b/examples/store.py index f1e3d932..5b09fd0f 100644 --- a/examples/store.py +++ b/examples/store.py @@ -1,16 +1,20 @@ import asyncio +from pathlib import Path +from typing import Optional import click from aleph_message.models import StoreMessage from aleph_message.status import MessageStatus -from mypy.dmypy_server import MiB from aleph.sdk.chains.common import get_fallback_private_key from aleph.sdk.chains.ethereum import ETHAccount from aleph.sdk.client import AuthenticatedAlephClient from aleph.sdk.conf import settings +from aleph.sdk.types import Account, StorageEnum +from aleph_message.utils import Mebibytes DEFAULT_SERVER = "https://api2.aleph.im" +MiB = 2**20 async def print_output_hash(message: StoreMessage, status: MessageStatus): @@ -55,30 +59,36 @@ async def do_upload(account, engine, channel, filename=None, file_hash=None): await print_output_hash(message, status) -async def do_upload_with_message(account, engine, channel, filename=None, item_hash=None): +async def do_upload_with_message( + account: Account, + engine: StorageEnum, + channel: str, + filename: Optional[str] = None, + item_hash: Optional[str] = None, +): async with AuthenticatedAlephClient( account=account, api_server=settings.API_HOST ) as session: print(filename, account.get_address()) if filename: try: - with open(filename, "rb") as f: - # Do something with the file - content = f.read() - if len(content) > 1000 * MiB and engine == "STORAGE": - print("File too big for native STORAGE engine") - return - message = await session.create_store_with_message( - file_content=content, - channel=channel, - storage_engine=engine.lower(), - file_hash=item_hash - ) - return message - except IOError: + p = Path(filename) + content = p.read_bytes() + if len(content) > 1000 * MiB and engine == "STORAGE": + print("File too big for native STORAGE engine") + return + message = await session.create_store_with_message( + file_content=content, + channel=channel, + storage_engine=engine.lower(), + file_hash=item_hash, + ) + return message + except Exception as e: print("File not accessible") raise + @click.command() @click.argument( "filename",