Skip to main content

Python SDK for 0G Storage

Project description

0G Storage Python SDK

This repository provides a Python SDK for interacting with 0G Storage.

Installation

  • Requires Python 3.9+
  • Install (editable): pip install -e .
  • Dev deps: pip install -e .[test]

Quick Start (Upload: Indexer or Node)

import os

from zg_storage import EvmClient
from zg_storage.indexer import IndexerClient
from zg_storage.transfer import NodeUploader, NodeUploaderConfig, UploadOption

USE_INDEXER = True

private_key = os.environ["PRIVATE_KEY"]
evm = EvmClient(
    rpc_url="https://evmrpc-testnet.0g.ai",
    private_key=private_key,
)

if USE_INDEXER:
    indexer = IndexerClient(
        "https://indexer-storage-testnet-turbo.0g.ai",
        evm_client=evm,
        flow_address="0x...",
    )
    tx_hash, root_hex = indexer.upload(file_path="your_file.dat", tags=b"", option=UploadOption())
else:
    cfg = NodeUploaderConfig(
        nodes=["http://node-1:5678", "http://node-2:5678"],
        evm_client=evm,
        flow_address="0x...",
    )
    uploader = NodeUploader.from_config(cfg)
    tx_hash, root_hex = uploader.upload(file_path="your_file.dat", tags=b"", option=UploadOption())

print("tx:", tx_hash)
print("root:", root_hex)

Download (Indexer or Node List)

from zg_storage.indexer import IndexerClient
from zg_storage.transfer import DownloadOption, NodeDownloader, NodeDownloaderConfig

USE_INDEXER = True
root = "0x..."
target = "downloaded.bin"

opt = DownloadOption()

if USE_INDEXER:
    indexer = IndexerClient("https://indexer-storage-testnet-turbo.0g.ai")
    indexer.download(root=root, filename=target, option=opt)
else:
    cfg = NodeDownloaderConfig(
        nodes=["http://node-1:5678", "http://node-2:5678"],
    )
    downloader = NodeDownloader.from_config(cfg)
    downloader.download(root=root, target=target, option=opt)

For all detailed parameters, advanced options, and latest endpoint/config guidance, see https://doc.0g.ai.

KV Write (Memory Upload)

import os

from zg_storage import EvmClient
from zg_storage.indexer import IndexerClient
from zg_storage.kv import StreamDataBuilder
from zg_storage.kv.types import create_tags
from zg_storage.core.data import BytesDataSource
from zg_storage.transfer import UploadOption

private_key = os.environ["PRIVATE_KEY"]
evm = EvmClient(rpc_url="https://evmrpc-testnet.0g.ai", private_key=private_key)

indexer = IndexerClient(
    "https://indexer-storage-testnet-turbo.0g.ai",
    evm_client=evm,
    flow_address="0x...",
)

builder = StreamDataBuilder()
builder.set(
    stream_id="0x" + "00" * 32,  # dummy stream id
    key=b"demo_key",             # dummy key
    data=b"demo_value",          # dummy value
)
stream_data = builder.build(sorted_items=True)
payload = stream_data.encode()
tags = create_tags(builder.stream_ids(), sorted_ids=True)

opt = UploadOption(tags=tags)
tx_hash, root_hex = indexer.upload(
    file_path=BytesDataSource(payload),
    tags=tags,
    option=opt,
)

KV Read

from zg_storage.kv import KvClient

kv = KvClient("http://your-kv-node:6789")
data = kv.get_values(
    "0x" + "00" * 32,  # dummy stream id
    ["demo_key"],      # dummy keys
)
print(data)

Development

  • Requires Python 3.9+
  • Install dev deps: pip install -e .[test]
  • Run tests: pytest

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

zg_storage-0.1.0.tar.gz (41.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zg_storage-0.1.0-py3-none-any.whl (42.1 kB view details)

Uploaded Python 3

File details

Details for the file zg_storage-0.1.0.tar.gz.

File metadata

  • Download URL: zg_storage-0.1.0.tar.gz
  • Upload date:
  • Size: 41.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for zg_storage-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e42180102b725b1f1cd9a7d8a9afb1efd711cddda87f02c1811d8f9ba64c89fa
MD5 69e43c005642eb2082f6e7b84048d6c7
BLAKE2b-256 73dd2b5a46a412e219f219883e7521f83ced672a98c7332981a5a60865dd3f62

See more details on using hashes here.

File details

Details for the file zg_storage-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: zg_storage-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 42.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for zg_storage-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5194f1897b63041b7a963c77a043009334cc07db2b56c2bd37e80fb07fbe1725
MD5 997dca8b228b476bda2accd4553601da
BLAKE2b-256 1648be037517680e03e672ada7cdf67845d1678496b2ccff5f99f143edbc2308

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