Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download file from IPFS in Python

Tags:

python

ipfs

I am using a scenario where uploading encrypted file to the ipfs, now I wanted it to download and then decrypt. Is there any way to download file using python?

like image 857
Deepthi Kr Avatar asked Oct 26 '25 08:10

Deepthi Kr


2 Answers

you can use python wrapper over IPFS to interact with IPFS. you can download it from here

https://github.com/ipfs/py-ipfs-api.git

import ipfsapi
api = ipfsapi.connect('127.0.0.1', 5001)

To add a file

new_file = api.add('new.txt')

The response will be like

 {'Name': 'new.txt', 'Hash': 'QmWvgsuZkaWxN1iC7GDciEGsAqphmDyCsk3CVHh7XVUUHq', 'Size': '28'}

In order to see the content of file you call

 api.cat('QmWvgsuZkaWxN1iC7GDciEGsAqphmDyCsk3CVHh7XVUUHq')
like image 78
GraphicalDot Avatar answered Oct 28 '25 23:10

GraphicalDot


I think this will help:

  1. first install ipfs python library: pip install ipfs-api
  2. connect to ipfs peer. This assume that you have installed ipfs on your machine and the ipfs daemon is running OR instead connect via infura with no need of running a daemon. This also depends how you uploaded the file.
  3. Once you are connected to the peer now use get function with ipfs hash as an inputs to download ipfs file
  4. Than you are done and the file will be stored in the current directory(where your python script is located)

for more can use the documantation https://media.readthedocs.org/pdf/python-ipfs-api/latest/python-ipfs-api.pdf.

Here is the code:

import ipfsApi
api = ipfsApi.Client(host='https://ipfs.infura.io', port=5001)
#OR
#api = ipfsApi.Client(host='http://127.0.0.1', port=5001)
api.get('Qm... Your IPFS Hash')
like image 24
Njabulo Avatar answered Oct 28 '25 22:10

Njabulo