gpt4 book ai didi

python - 如何在 Python 中将多个文件添加到 IPFS?

转载 作者:行者123 更新时间:2023-12-05 03:42:59 24 4
gpt4 key购买 nike

我是一个尝试从 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 dict 的大小来一次添加多个文件:

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']))

关于python - 如何在 Python 中将多个文件添加到 IPFS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67036034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com