Using ipfspy.ipfspec to interact with IPFS

A Tutorial on using ipfspy.ipfsspec
import fsspec
import os
from ipfspy.ipfsspec.asyn import AsyncIPFSFileSystem
from fsspec import register_implementation
import asyncio
import io
# 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

Put

Put File

# put in a file and store it into test folder 'fam'
fs.ipfs.rm('/test_put_file')
put_file_res = fs.ipfs.put(path='output/test.txt', rpath='/test_put_file')
fs.ipfs.ls('/test_put_file')
[{'name': '/test_put_file/test.txt',
  'CID': 'QmbwFKzLj9m2qwBFYTVrBotf4QujvzTb1GBV9wFcNPMctm',
  'type': 'file',
  'size': 20}]

Put Folder

# put a directory and store it into test folder
fs.ipfs.put(path='output/fol1/fol2', rpath='/test_put_folder' ,recursive=True, return_cid=False)
fs.ipfs.ls('/')
[{'name': '/test_dataset',
  'CID': 'QmXuV4QHUPyonn82iotczv9pWQndcUt7PFMsJWtTN2Cb1J',
  'type': 'directory',
  'size': 0},
 {'name': '/test_get_folder',
  'CID': 'QmRWz2opj9To63PX4Jt9uYs39FppGrfbui1BkQjbjmxRiQ',
  'type': 'directory',
  'size': 0},
 {'name': '/test_model',
  'CID': 'QmZCsKFtvCWr3opi5mkS8Vm288QCD6sxEnUwC2L91knc2r',
  'type': 'directory',
  'size': 0},
 {'name': '/test_put_file',
  'CID': 'QmcSHBc2XV69PXy6nLQFrHyuNH25JL39VhUYQakiYGn86y',
  'type': 'directory',
  'size': 0},
 {'name': '/test_put_folder',
  'CID': 'QmRWz2opj9To63PX4Jt9uYs39FppGrfbui1BkQjbjmxRiQ',
  'type': 'directory',
  'size': 0},
 {'name': '/test_rm_file',
  'CID': 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
  'type': 'directory',
  'size': 0}]

Cat

fs.ipfs.ls('/test_put_folder')
[{'name': '/test_put_folder/test.txt',
  'CID': 'QmbwFKzLj9m2qwBFYTVrBotf4QujvzTb1GBV9wFcNPMctm',
  'type': 'file',
  'size': 20},
 {'name': '/test_put_folder/test2.txt',
  'CID': 'QmZCFrtagSLhiHKAywF8oWxapXtR6JiJ8GeENASyhLvvyu',
  'type': 'file',
  'size': 20},
 {'name': '/test_put_folder/test3.txt',
  'CID': 'QmWT5UmZ4zoXX9GsXpPtEMVP5m1VZ7N6rdxnXHGNkFKqFJ',
  'type': 'file',
  'size': 28}]
# file
print(fs.ipfs.cat_file('/test_put_folder/test.txt'))
print(fs.ipfs.cat_file('QmZCFrtagSLhiHKAywF8oWxapXtR6JiJ8GeENASyhLvvyu'))
b"\n'''\nFirst file\n'''\n"
b'```\nsecond file\n```\n'
# folder
print(fs.ipfs.cat('QmRWz2opj9To63PX4Jt9uYs39FppGrfbui1BkQjbjmxRiQ'))
print(fs.ipfs.cat('/test_put_folder'))
{'QmRWz2opj9To63PX4Jt9uYs39FppGrfbui1BkQjbjmxRiQ/test.txt': b"\n'''\nFirst file\n'''\n", 'QmRWz2opj9To63PX4Jt9uYs39FppGrfbui1BkQjbjmxRiQ/test2.txt': b'```\nsecond file\n```\n', 'QmRWz2opj9To63PX4Jt9uYs39FppGrfbui1BkQjbjmxRiQ/test3.txt': b"\n'''\nthis is third file\n'''\n"}
{'/test_put_folder/test.txt': b"\n'''\nFirst file\n'''\n", '/test_put_folder/test2.txt': b'```\nsecond file\n```\n', '/test_put_folder/test3.txt': b"\n'''\nthis is third file\n'''\n"}

Remove Folder

print(fs.ipfs.put(path='output/fol1/fol2', rpath='/test_rm_folder' ,recursive=True, return_cid=True))
print('Before: ',fs.ipfs.ls('/test_rm_folder', detail=False))
fs.ipfs.rm('/test_rm_folder')
print('After: ',fs.ipfs.ls('/test_rm_folder', detail=False))
QmRWz2opj9To63PX4Jt9uYs39FppGrfbui1BkQjbjmxRiQ
Before:  ['/test_rm_folder/test.txt', '/test_rm_folder/test2.txt', '/test_rm_folder/test3.txt']
After:  []

Remove File

fs.ipfs.put(path='output/test.txt', rpath='/test_rm_file' ,recursive=True, return_cid=False)
print('Before: ',fs.ipfs.ls('/test_rm_file', detail=False))

fs.ipfs.rm('/test_rm_file/test.txt')
print('After: ',fs.ipfs.ls('/test_rm_file', detail=False))
Before:  ['/test_rm_file/test.txt']
After:  []

Get Function

Get Remote File

if fs.file.exists('output/get/test.txt'):
    fs.file.rm('output/get/test.txt')
    
fs.ipfs.put(path='output/test.txt', rpath='/test_get_file' ,recursive=True, return_cid=False)
print('Before: ', [p.lstrip(os.getcwd()) for p in fs.file.glob('output/get/*.txt')])

fs.ipfs.get(rpath='/test_get_file/test.txt', lpath='output/get/test.txt',recursive=True, return_cid=False)
print('After: ', [p.lstrip(os.getcwd()) for p in fs.file.glob('output/get/*.txt')])
fs.ipfs.rm('/test_get_file')
Before:  []
After:  ['output/get/test.txt']
['/test_get_file']

Get Remote Folder

if fs.file.exists('output/get'):
    fs.file.rm('output/get', recursive=True)
    
fs.ipfs.put(lpath='output/fol1/fol2', rpath='/test_get_folder')
print('Before: ', [p.lstrip(os.getcwd()) for p in fs.file.glob('output/get/*.txt')])
fs.ipfs.get(rpath='/test_get_folder', lpath='output/get',recursive=True, return_cid=False)
print('After: ', [p.lstrip(os.getcwd()) for p in fs.file.glob('output/get/*.txt')])
Before:  []
After:  ['output/get/test.txt', 'output/get/test2.txt', 'output/get/test3.txt']