如何在Python中向IPFS添加多个文件



我是一个尝试从IPFS网络上传和访问数据的初学者。我目前正在学习这方面的教程。这是他们网站上的代码。

# upload
import requests
import json
files = {
'fileOne': ('Congrats! This is the first sentence'),
}
response = requests.post('https://ipfs.infura.io:5001/api/v0/add', files=files)
p = response.json()
hash = p['Hash']
print(hash)
# retreive
params = (
('arg', hash),
)
response_two = requests.post('https://ipfs.infura.io:5001/api/v0/block/get', params=params)
print(response_two.text)

我试过了,效果很好。然而,不是像这样只包含1个记录的1个文件-

files = {
'fileOne': ('Congrats! This is the first sentence'),
}

我想上传多个这样的文件。例如,由多行数据组成的员工详细信息。我一直在尝试多种方法,但都没有成功。有人能介绍一下怎么做吗?非常感谢。

只需增加filesdict:的大小,就可以同时添加多个文件

files = {
'fileOne': ('Congrats! This is the first sentence'),
'fileTwo': ('''Congrats! This is the first sentence
This is the second sentence.'''),
'example': (open('example', 'rb')),
}

响应是一个JSON流,每个条目由一行新行分隔。因此,您必须以不同的方式解析响应:

dec = json.JSONDecoder()
i = 0
while i < len(response.text):
data, s = dec.raw_decode(response.text[i:])
i += s+1
print("%s: %s" % (data['Name'], data['Hash']))

最新更新