如何在 Python 中将多个文件添加到 IPFS?
How to add multiple files to IPFS in Python?
我是一个尝试从 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'),
}
我想上传多个这样的文件。例如,由多行数据组成的员工详细信息。我一直在尝试多种方法,但都没有成功。有人可以提到如何做到这一点吗?谢谢。
您只需增加 files
字典的大小即可一次添加多个文件:
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']))
我是一个尝试从 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'),
}
我想上传多个这样的文件。例如,由多行数据组成的员工详细信息。我一直在尝试多种方法,但都没有成功。有人可以提到如何做到这一点吗?谢谢。
您只需增加 files
字典的大小即可一次添加多个文件:
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']))