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.2.0.tar.gz (142.3 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.2.0-py3-none-any.whl (146.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: zg_storage-0.2.0.tar.gz
  • Upload date:
  • Size: 142.3 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.2.0.tar.gz
Algorithm Hash digest
SHA256 cb25223ffbbfb476961f056494023f6f9f01d9e3d8d7437816281967cff9553e
MD5 cb1b9fc5a57d11721eb01673c116f688
BLAKE2b-256 73614efa5f52a3ad58e4da53b36945a0e2be28f249ae93b2e9b0c8a6b79bc22d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zg_storage-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 146.8 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba4e58525a2b3361b79ef061a9adbce76a0492153f6763287546244304b42a5d
MD5 6bd3dd5979d1abab41dc5129ad722229
BLAKE2b-256 eaeb765f6a4f7843d65d6ab222a1928b8eaa69a9734be01a6922f8eb9d963754

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