I'd like to override the upload behavior of a django ImageField so that after uploading to some url, the file will be added to an ipfs node.
For example, my model is something like:
class Profile(models.Model):
picture = models.ImageField(upload_to=upload_location, blank=True)
I first try saving it as you would any other image, but then I give it an IPFS hash, which will allow the user to load the data client side.
In my views I have the following code to get an instance of ipfs daemon running.
import ipfsapi
from subprocess import call
os.system("ipfs daemon")
api = ipfsapi.connect('127.0.0.1', 5001)
When I try to run python manage.py makemigrations
or runserver
, however, the daemon runs but the rest of the command doesn't.
Initializing daemon...
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/174.56.29.92/tcp/4001
Swarm listening on /ip4/192.168.1.109/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready
How can I start an ipfs daemon and also a django server? It doesn't look like they're listening on the same port (Django 8000, IPFS 8080), so why am I running into this problem?
https://pydigger.com/pypi/django-ipfs-storage
.. code:: bash
pip install django-ipfs-storage
By default ipfs_storage
adds and pins content to an IPFS daemon
running on localhost and returns URLs pointing to the public
https://ipfs.io/ipfs/ HTTP Gateway
To customise this, set the following variables in your settings.py
:
IPFS_STORAGE_API_URL
: defaults to
'http://localhost:5001/api/v0/'
.IPFS_GATEWAY_API_URL
: defaults to 'https://ipfs.io/ipfs/'
.Set IPFS_GATEWAY_API_URL
to 'http://localhost:8080/ipfs/'
to
serve content through your local daemon’s HTTP gateway.
There are two ways to use a Django storage backend.
As default backend ~~~~~~~~~~~~~~~~~~
Use IPFS as Django’s default file storage
backend <https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-DEFAULT_FILE_STORAGE>
__:
.. code:: python
# settings.py
DEFAULT_FILE_STORAGE = 'ipfs_storage.InterPlanetaryFileSystemStorage'
IPFS_STORAGE_API_URL = 'http://localhost:5001/api/v0/'
IPFS_STORAGE_GATEWAY_URL = 'http://localhost:8080/ipfs/'
For a specific FileField ~~~~~~~~~~~~~~~~~~~~~~~~
Alternatively, you may only want to use the IPFS storage backend for a single field:
.. code:: python
from django.db import models
from ipfs_storage import InterPlanetaryFileSystemStorage
class MyModel(models.Model):
# …
file_stored_on_ipfs = models.FileField(storage=InterPlanetaryFileSystemStorage())
other_file = models.FileField() # will still use DEFAULT_FILE_STORAGE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With