Uh oh!
There was an error while loading. Please reload this page.
forked from CodeWithJoe2020/ipfsPython
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.py
More file actions
Latest commit
26 lines (22 loc) · 813 Bytes
/
Copy pathupload.py
File metadata and controls
26 lines (22 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
importrequests
importjson
# IPFS gateway and API endpoints
gateway_url='http://localhost:8080'
api_url='http://localhost:5001/api/v0'
# Add a file to IPFS
defadd_file(filename):
withopen(filename, 'rb') asf:
files= {'file': f}
response=requests.post(f'{api_url}/add', files=files)
file_hash=json.loads(response.text)['Hash']
print(f'Added file to IPFS with CID: {file_hash}')
returnfile_hash
# Retrieve a file from IPFS
defget_file(file_hash, output_file):
response=requests.get(f'{gateway_url}/ipfs/{file_hash}')
withopen(output_file, 'wb') asf:
f.write(response.content)
print(f'File retrieved from IPFS and saved as {output_file}')
# Test the functions
file_hash=add_file('myfile.txt')
get_file(file_hash, 'output.txt')