ipfshttpclient安装
# 下载git上资源 git clone https://github.com/ipfs/py-ipfs-http-client.git cd py-ipfs-http-client # 将ipfs-api-client链接到你的Python路径 vim /etc/profile source /etc/profile FLIT_ROOT_INSTALL=1 # 添加环境变量 python3.6 -m pip install flit # 安装filt flit install --pth-file # 添加到Python路径路径中
ipfs python接口
import json import ipfshttpclient class IPFSApiClient: def __init__(self): self.api = ipfshttpclient.connect('/ip4/192.168.107.216/tcp/5001/http') # /ip4/192.168.107.220/tcp/9095 # self.api = ipfshttpclient.connect('/ip4/192.168.107.220/tcp/9095/http') def file_add(self, file_path): # 上传文件 response = self.api.add(file_path) print(response) return response def file_addr(self, file_path): # 上传文件夹 response = self.api.add(file_path, recursive=True, wrap_with_directory=True, pin=True) print(response) return response def file_mkdir(self, file_path): # 创建文件夹 try: self.api.files.mkdir(file_path) # 移动文件 except: print("文件夹已经创建") def file_cp(self, file_path, catalogue): # 移动文件 try: self.api.files.cp(file_path, catalogue) except: print("文件一已经存在") def file_stat(self, file_path): # 查看文件信息 response = self.api.files.stat(file_path) # 获取文件哈希 print(response) return response def connection_url(self, url, fime_name=''): if fime_name: print("http://192.168.107.216:8080/ipfs/{}/{}".format(url, fime_name)) else: print("http://192.168.107.216:8080/ipfs/{}".format(url)) if __name__ == '__main__': client = IPFSApiClient() catalogue = "/740" fime_name = 'a.jpg' hash = client.file_add("./740/730_ss_ffe584c163a2b16e9c1b733b1c8e2ba669fb1204.jpg")['Hash'] client.file_mkdir(catalogue) print("/ipfs/{}".format(hash), '{}/a,jpg'.format(catalogue)) client.file_cp("/ipfs/{}".format(hash), '{}/{}'.format(catalogue, fime_name)) file_hash = client.file_stat(catalogue)['Hash'] client.connection_url(hash) client.connection_url(file_hash, fime_name)
import io import json import ipfshttpclient class ipfs_test(object): def __init__(self): # 连接IPFS,需要先启动节点服务器daemon self.api = ipfshttpclient.connect('/ip4/192.168.107.216/tcp/5001/http') def ipfs_id(self): ipf = self.api.id() # 查看节点ID ipf = self.api.add('jp.jpg') # 上传文件 ipf = self.api.files_mkdir('/fake_dir') # 上传目录 ipf = self.api.cat('QmecSv31G89AgE1KXrWuwHMvq1xUjLRYaKpww247j1BXij') # 查看文件内容 ipf = self.api.pin.ls(type='all') # 查看本地已存储的所有文件 ipf = self.api.files_cp("/test", "/bla") # 复制 ipf = self.api.files_ls("/") # 遍历文件 ipf = self.api.files_rm("/fake_dir") # 删除一个文件 ipf = self.api.files_read("/fake_dir") # 读取一个文件的内容 ipf = self.api.files_mv("/test/file", "/bla/file") # 移动一个文件 ipf = self.api.files_write("/test/file", io.BytesIO(b"hi"), create=True) # 对一个文件进行写操作 print(json.dumps(ipf, ensure_ascii=False)) if __name__ == "__main__": ipf = ipfs_test() ipf.ipfs_id()
1.基本用法 (需要已经有一个运行的 IPFS 服务实例)
import ipfshttpclient api = ipfshttpclient.connect('/ip4/192.168.107.216/tcp/5001/http') res = api.add('test.txt') res {'Hash': 'QmWxS5aNTFEc9XbMX1ASvLET1zrqEaTssqt33rVZQCQb22', 'Name': 'test.txt'} api.cat(res['Hash']) 'fdsafkljdskafjaksdjf\n'
2.管理功能
api.id() {'Addresses': ['/ip4/127.0.0.1/tcp/4001/ipfs/QmS2C4MjZsv2iP1UDMMLCYqJ4WeJw8n3vXx1VKxW1UbqHS', '/ip6/::1/tcp/4001/ipfs/QmS2C4MjZsv2iP1UDMMLCYqJ4WeJw8n3vXx1VKxW1UbqHS'], 'AgentVersion': 'go-ipfs/0.4.10', 'ID': 'QmS2C4MjZsv2iP1UDMMLCYqJ4WeJw8n3vXx1VKxW1UbqHS', 'ProtocolVersion': 'ipfs/0.1.0', 'PublicKey': 'CAASpgIwgg ... 3FcjAgMBAAE='}
3.传入API参数
api.pin_ls(type='all') {'Keys': {'QmNMELyizsfFdNZW3yKTi1SE2pErifwDTXx6vvQBfwcJbU': {'Count': 1, 'Type': 'indirect'}, 'QmNQ1h6o1xJARvYzwmySPsuv9L5XfzS4WTvJSTAWwYRSd8': {'Count': 1, 'Type': 'indirect'}, …
4.添加目录,指定文件名类型匹配
api.add('photos', match='*.jpg') [{'Hash': 'QmcqBstfu5AWpXUqbucwimmWdJbu89qqYmE3WXVktvaXhX', 'Name': 'photos/photo1.jpg'}, {'Hash': 'QmSbmgg7kYwkSNzGLvWELnw1KthvTAMszN5TNg3XQ799Fu', 'Name': 'photos/photo2.jpg'}, {'Hash': 'Qma6K85PJ8dN3qWjxgsDNaMjWjTNy8ygUWXH2kfoq9bVxH', 'Name': 'photos/photo3.jpg'}]
5.递归添加目录
api.add('fake_dir', recursive=True) [{'Hash': 'QmQcCtMgLVwvMQGu6mvsRYLjwqrZJcYtH4mboM9urWW9vX', 'Name': 'fake_dir/fsdfgh'}, {'Hash': 'QmNuvmuFeeWWpxjCQwLkHshr8iqhGLWXFzSGzafBeawTTZ', 'Name': 'fake_dir/test2/llllg'}, {'Hash': 'QmX1dd5DtkgoiYRKaPQPTCtXArUu4jEZ62rJBUcd5WhxAZ', 'Name': 'fake_dir/test2'}, {'Hash': 'Qmenzb5J4fR9c69BbpbBhPTSp2Snjthu2hKPWGPPJUHb9M', 'Name': 'fake_dir'}]
6.辅助函数,添加字符串和JSON等资源
lst = [1, 77, 'lol'] client.add_json(lst) 'QmQ4R5cCUYBWiJpNL7mFe4LDrwD6qBr5Re17BoRAY9VNpd' client.get_json(_) [1, 77, 'lol']
7.综合
import ipfshttpclient # 连接IPFS,需要先启动节点服务器daemon api = ipfshttpclient.connect('/ip4/192.168.107.216/tcp/5001/http') # 查看节点ID api.id() # 上传文件 res = api.add('test.txt') # 上传目录 res = api.add('pub_dir', recursive=True) # 查看文件内容 res = api.cat('QmWxS5aNTFEc9XbMX1ASvLET1zrqEaTssqt33rVZQCQb23') # 下载文件 res = api.get('QmWxS5aNTFEc9XbMX1ASvLET1zrqEaTssqt33rVZQCQb23')
https://ipfs.io/ipns/QmZ86ow1byeyhNRJEatWxGPJKcnQKG7s51MtbHdxxUddTH/Software/Python/ipfsapi/api_ref.html