Skip to content

Commit

Permalink
Fix: exemple
Browse files Browse the repository at this point in the history
  • Loading branch information
1yam committed Aug 29, 2023
1 parent 6d3a18a commit 8027f98
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions examples/store.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 8027f98

Please sign in to comment.