import fsspec
import os
from ipfspy.ipfsspec.asyn import AsyncIPFSFileSystem
from fsspec import register_implementation
import asyncio
import io
Using ipfspy.ipfspec
to interact with IPFS
A Tutorial on using
ipfspy.ipfsspec
# register_implementation(IPFSFileSystem.protocol, IPFSFileSystem)
register_implementation(AsyncIPFSFileSystem.protocol, AsyncIPFSFileSystem)
# with fsspec.open("ipfs://QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx", "r") as f:
# print(f.read())
class fs:
= fsspec.filesystem("ipfs")
ipfs file = fsspec.filesystem("file")
Changed to local node
Put
Put File
# put in a file and store it into test folder 'fam'
'/test_put_file')
fs.ipfs.rm(= fs.ipfs.put(path='output/test.txt', rpath='/test_put_file')
put_file_res '/test_put_file') fs.ipfs.ls(
[{'name': '/test_put_file/test.txt',
'CID': 'QmbwFKzLj9m2qwBFYTVrBotf4QujvzTb1GBV9wFcNPMctm',
'type': 'file',
'size': 20}]
Put Folder
# put a directory and store it into test folder
='output/fol1/fol2', rpath='/test_put_folder' ,recursive=True, return_cid=False)
fs.ipfs.put(path'/') 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
'/test_put_folder') fs.ipfs.ls(
[{'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))
'/test_rm_folder')
fs.ipfs.rm(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
='output/test.txt', rpath='/test_rm_file' ,recursive=True, return_cid=False)
fs.ipfs.put(pathprint('Before: ',fs.ipfs.ls('/test_rm_file', detail=False))
'/test_rm_file/test.txt')
fs.ipfs.rm(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'):
file.rm('output/get/test.txt')
fs.
='output/test.txt', rpath='/test_get_file' ,recursive=True, return_cid=False)
fs.ipfs.put(pathprint('Before: ', [p.lstrip(os.getcwd()) for p in fs.file.glob('output/get/*.txt')])
='/test_get_file/test.txt', lpath='output/get/test.txt',recursive=True, return_cid=False)
fs.ipfs.get(rpathprint('After: ', [p.lstrip(os.getcwd()) for p in fs.file.glob('output/get/*.txt')])
'/test_get_file') fs.ipfs.rm(
Before: []
After: ['output/get/test.txt']
['/test_get_file']
Get Remote Folder
if fs.file.exists('output/get'):
file.rm('output/get', recursive=True)
fs.
='output/fol1/fol2', rpath='/test_get_folder')
fs.ipfs.put(lpathprint('Before: ', [p.lstrip(os.getcwd()) for p in fs.file.glob('output/get/*.txt')])
='/test_get_folder', lpath='output/get',recursive=True, return_cid=False)
fs.ipfs.get(rpathprint('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']