如何使用Django将表单/字段内容上载到IPFS节点?

Dav*_* J. 5 python django port ipfs

我想覆盖django ImageField的上传行为,以便在上传到某个url后,该文件将被添加到ipfs节点.

例如,我的模型类似于:

class Profile(models.Model):
    picture = models.ImageField(upload_to=upload_location, blank=True)
Run Code Online (Sandbox Code Playgroud)

我首先尝试保存它,就像任何其他图像一样,但是我给它一个IPFS哈希,这将允许用户加载数据客户端.

在我的视图中,我有以下代码来获取运行ipfs守护程序的实例.

import ipfsapi
from subprocess import call

os.system("ipfs daemon")
api = ipfsapi.connect('127.0.0.1', 5001)
Run Code Online (Sandbox Code Playgroud)

当我尝试运行时,python manage.py makemigrations或者runserver守护程序运行但命令的其余部分没有运行.

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
Run Code Online (Sandbox Code Playgroud)

如何启动ipfs守护进程和django服务器?它看起来不像是在同一个端口上监听(Django 8000,IPFS 8080),为什么我遇到这个问题呢?

mul*_*van 1

https://pydigger.com/pypi/django-ipfs-storage

\n\n

安装

\n\n

.. 代码:: bash

\n\n
pip install django-ipfs-storage\n
Run Code Online (Sandbox Code Playgroud)\n\n

配置

\n\n

默认情况下,ipfs_storage将内容添加并固定到在本地主机上运行的 IPFS 守护程序\n并返回指向公共的 URL\n https://ipfs.io/ipfs/ HTTP 网关

\n\n

要自定义此设置,请在您的 中设置以下变量settings.py

\n\n
    \n
  • IPFS_STORAGE_API_URL:默认为\n \'http://localhost:5001/api/v0/\'
  • \n
  • IPFS_GATEWAY_API_URL: 默认为\'https://ipfs.io/ipfs/\'.
  • \n
\n\n

设置IPFS_GATEWAY_API_URL\'http://localhost:8080/ipfs/\'\n通过本地守护程序\xe2\x80\x99s HTTP 网关提供内容。

\n\n

用法

\n\n

有两种使用 Django 存储后端的方法。

\n\n

作为默认后端\n~~~~~~~~~~~~~~~~~~

\n\n

使用 IPFS 作为Django\xe2\x80\x99s default file storage\nbackend <https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-DEFAULT_FILE_STORAGE>__:

\n\n

.. 代码:: python

\n\n
# settings.py\n\nDEFAULT_FILE_STORAGE = \'ipfs_storage.InterPlanetaryFileSystemStorage\'\n\nIPFS_STORAGE_API_URL = \'http://localhost:5001/api/v0/\'\nIPFS_STORAGE_GATEWAY_URL = \'http://localhost:8080/ipfs/\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于特定的 FileField\n~~~~~~~~~~~~~~~~~~~~~~~~

\n\n

或者,您可能只想将 IPFS 存储后端用于单个字段:\n

\n\n

.. 代码:: python

\n\n
from django.db import models\n\nfrom ipfs_storage import InterPlanetaryFileSystemStorage \n\n\nclass MyModel(models.Model):\n    # \xe2\x80\xa6\n    file_stored_on_ipfs = models.FileField(storage=InterPlanetaryFileSystemStorage()) \n    other_file = models.FileField()  # will still use DEFAULT_FILE_STORAGE\n
Run Code Online (Sandbox Code Playgroud)\n