I am a beginner trying to upload and access data from an IPFS network. I am currently going through a tutorial on the same. This is the code on their site.
# 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)
I tried it and it is working fine. However, instead of just 1 file containing 1 record like this-
files = {
'fileOne': ('Congrats! This is the first sentence'),
}
I want to upload multiple such files. For example, employee details which consist of multiple rows of data. I have been trying multiple ways but have been unsuccessful. Can someone mention how to do this? Thank you.
You can add multiple files at once by simply increasing the size of your 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')),
}
The response is a JSON stream, with each entry separated by a new line. So you'll have to parse the response a bit differently:
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']))