5

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

例如,我的模型是这样的:

class Profile(models.Model):
    picture = models.ImageField(upload_to=upload_location, blank=True)

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

在我看来,我有以下代码来运行 ipfs 守护程序的实例。

import ipfsapi
from subprocess import call

os.system("ipfs daemon")
api = ipfsapi.connect('127.0.0.1', 5001)

但是,当我尝试运行python manage.py makemigrationsorrunserver时,守护程序会运行,但命令的其余部分不会。

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

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

4

1 回答 1

0

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

安装

.. 代码:: 重击

pip install django-ipfs-storage

配置

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

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

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

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

用法

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

作为默认后端~~~~~~~~~~~~~~~~~~

使用 IPFS 作为Django’s default file storage backend <https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-DEFAULT_FILE_STORAGE>__:

.. 代码:: 蟒蛇

# 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/'

对于特定的 FileField ~~~~~~~~~~~~~~~~~~~~~~~~

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

.. 代码:: 蟒蛇

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
于 2018-11-09T23:39:27.663 回答