IPFSSpec

asyn implementation of ipfsspec for fsspec

source

RequestsTooQuick

 RequestsTooQuick (retry_after=None)

Base class for I/O related errors.


source

AsyncRequestSession

 AsyncRequestSession (loop=None, adapter={'pool_connections': 100,
                      'pool_maxsize': 100}, **kwargs)

Initialize self. See help(type(self)) for accurate signature.


source

AsyncIPFSFileSystem

 AsyncIPFSFileSystem (*args, **kwargs)

Async file operations, default implementations

Passes bulk operations to asyncio.gather for concurrent operation.

Implementations that have concurrent batch operations and/or async methods should inherit from this class instead of AbstractFileSystem. Docstrings are copied from the un-underscored method in AbstractFileSystem, if not given.

Changing Gateways

To learn more about the fsspec api, please see the dock here

Register ipfsspec to fsspec,

import fsspec, os, io, glob, asyncio
from ipfspy.ipfsspec.asyn import AsyncIPFSFileSystem
from fsspec import register_implementation

# register_implementation(IPFSFileSystem.protocol, IPFSFileSystem)
register_implementation(AsyncIPFSFileSystem.protocol, AsyncIPFSFileSystem)

# with fsspec.open("ipfs://QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx", "r") as f:
#     print(f.read())
class fs:
    ipfs = fsspec.filesystem("ipfs")
    file = fsspec.filesystem("file")
Changed to local node
fs.ipfs.change_gateway_type = 'public'
Changed to public node
fs.ipfs.change_gateway_type = 'local'
Changed to local node

Using Local node

Put File

fs.ipfs.put(path='output/test.txt', rpath='/test_put_file')
'QmbwFKzLj9m2qwBFYTVrBotf4QujvzTb1GBV9wFcNPMctm'

Put Folder

fs.ipfs.put(path='output/fol1/fol2', rpath='/test_put_folder', recursive=True, return_cid=False)
[{'Name': 'fol2/test2.txt',
  'Hash': 'QmZCFrtagSLhiHKAywF8oWxapXtR6JiJ8GeENASyhLvvyu',
  'Size': '28'},
 {'Name': 'fol2/test.txt',
  'Hash': 'QmbwFKzLj9m2qwBFYTVrBotf4QujvzTb1GBV9wFcNPMctm',
  'Size': '28'},
 {'Name': 'fol2/test3.txt',
  'Hash': 'QmWT5UmZ4zoXX9GsXpPtEMVP5m1VZ7N6rdxnXHGNkFKqFJ',
  'Size': '36'},
 {'Name': 'fol2/.ipynb_checkpoints',
  'Hash': 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
  'Size': '4'},
 {'Name': 'fol2',
  'Hash': 'QmWHpqeDLAMMuLSKDqcMNZRaZGwEKbY5FoU7DMA3pftj54',
  'Size': '312'}]

Cat File

fs.ipfs.cat('QmbwFKzLj9m2qwBFYTVrBotf4QujvzTb1GBV9wFcNPMctm')
b"\n'''\nFirst file\n'''\n"

Cat Folder

fs.ipfs.cat('QmWHpqeDLAMMuLSKDqcMNZRaZGwEKbY5FoU7DMA3pftj54')
{'QmWHpqeDLAMMuLSKDqcMNZRaZGwEKbY5FoU7DMA3pftj54/test.txt': b"\n'''\nFirst file\n'''\n",
 'QmWHpqeDLAMMuLSKDqcMNZRaZGwEKbY5FoU7DMA3pftj54/test2.txt': b'```\nsecond file\n```\n',
 'QmWHpqeDLAMMuLSKDqcMNZRaZGwEKbY5FoU7DMA3pftj54/test3.txt': b"\n'''\nthis is third file\n'''\n"}

List a folder

fs.ipfs.ls('QmWHpqeDLAMMuLSKDqcMNZRaZGwEKbY5FoU7DMA3pftj54')
[{'name': 'QmWHpqeDLAMMuLSKDqcMNZRaZGwEKbY5FoU7DMA3pftj54/.ipynb_checkpoints',
  'CID': 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
  'type': 'directory',
  'size': 0},
 {'name': 'QmWHpqeDLAMMuLSKDqcMNZRaZGwEKbY5FoU7DMA3pftj54/test.txt',
  'CID': 'QmbwFKzLj9m2qwBFYTVrBotf4QujvzTb1GBV9wFcNPMctm',
  'type': 'file',
  'size': 20},
 {'name': 'QmWHpqeDLAMMuLSKDqcMNZRaZGwEKbY5FoU7DMA3pftj54/test2.txt',
  'CID': 'QmZCFrtagSLhiHKAywF8oWxapXtR6JiJ8GeENASyhLvvyu',
  'type': 'file',
  'size': 20},
 {'name': 'QmWHpqeDLAMMuLSKDqcMNZRaZGwEKbY5FoU7DMA3pftj54/test3.txt',
  'CID': 'QmWT5UmZ4zoXX9GsXpPtEMVP5m1VZ7N6rdxnXHGNkFKqFJ',
  'type': 'file',
  'size': 28}]

Get a file

def test_get_public_cid(cid='QmWT5UmZ4zoXX9GsXpPtEMVP5m1VZ7N6rdxnXHGNkFKqFJ', 
                        rpath='/test_get_file',
                        out_lpath = 'output/get_file/', 
                        gateway_type=None):
    if gateway_type:
        fs.ipfs.change_gateway_type = 'local'
    if fs.file.exists(out_lpath):
        fs.file.rm(out_lpath, recursive=True)
        
    fs.ipfs.rm(rpath, recursive=True)
    print('Before: ', [p.lstrip(os.getcwd()) for p in fs.file.glob(f'{out_lpath}/*')])
    
    fs.ipfs.get(rpath=cid, lpath=out_lpath, recursive=True, return_cid=False)
    print('After: ', [p.lstrip(os.getcwd()) for p in fs.file.glob(f'{out_lpath}/*')])
    
    fs.ipfs.rm(rpath, recursive=True)

test_get_public_cid()
Before:  []
After:  ['output/get_file/QmWT5UmZ4zoXX9GsXpPtEMVP5m1VZ7N6rdxnXHGNkFKqFJ']