Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kralverde/py-ipfs-car-decoder
A way to decode IPFS CAR blocks in python and save them to disk or stream file-likes.
https://github.com/kralverde/py-ipfs-car-decoder
async car ipfs ipld python python3
Last synced: 10 days ago
JSON representation
A way to decode IPFS CAR blocks in python and save them to disk or stream file-likes.
- Host: GitHub
- URL: https://github.com/kralverde/py-ipfs-car-decoder
- Owner: kralverde
- Created: 2023-10-15T21:52:24.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-16T19:30:09.000Z (11 months ago)
- Last Synced: 2024-04-24T06:21:55.475Z (9 months ago)
- Topics: async, car, ipfs, ipld, python, python3
- Language: Python
- Homepage:
- Size: 23.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
Validate what an IPFS gateway returns by decoding and validating the CAR block.
Usage
-----CARs can be read from memory or file and can be exported as a file/folder or byte stream
>>> import asyncio
>>> import aiohttp
...
>>> from ipfs_car_decoder import ChunkedMemoryByteStream, stream_bytes
...
>>> ipfs_hash = 'bafkreibm6jg3ux5qumhcn2b3flc3tyu6dmlb4xa7u5bf44yegnrjhc4yeq'
...
>>> async def test():
... stream = ChunkedMemoryByteStream()
... async with aiohttp.ClientSession() as session:
... async with session.get(f'https://ipfs.io/ipfs/{ipfs_hash}', headers={'Accept':'application/vnd.ipld.car'}) as resp:
... resp.raise_for_status()
... assert resp.content_type == 'application/vnd.ipld.car'
... async for chunk, _ in resp.content.iter_chunks():
... await stream.append_bytes(chunk)
... await stream.mark_complete()
... acc = b''
... async for chunk in stream_bytes(ipfs_hash, stream):
... acc += chunk
... print(acc)
...
>>> asyncio.run(test())
b'hello'