【发布时间】:2026-01-08 23:50:01
【问题描述】:
我正在使用 django 存储模块将文件存储在使用 sftp 的位置,现在我们需要从 sftp 下载相同的文件。
有什么建议吗?
models.py
from storages.backends.sftpstorage import SFTPStorage
SFS = SFTPStorage()
class Configurations(BaseModel):
name = models.CharField(max_length=150, unique=True)
file = models.FileField(upload_to='configurations', storage=SFS)
descriptions = models.TextField(null=True, blank=True)
settings.py
DEFAULT_FILE_STORAGE = 'storages.backends.sftpstorage.SFTPStorage'
SFTP_STORAGE_HOST = '127.0.0.1'
SFTP_STORAGE_ROOT = '/var/www/media/'
SFTP_STORAGE_PARAMS = {
'username': 'root',
'password': 'password',
'allow_agent': False,
'look_for_keys': False,
}
# SFTP_KNOWN_HOST_FILE = '~/.ssh/known_hosts'
SFTP_STORAGE_INTERACTIVE = False
views.py
from django.http import Http404
from rest_framework import viewsets
from rest_framework.parsers import MultiPartParser, FormParser
from .models import (CustomConfigurations)
from .serializers import (CustomConfigurationsSerializers)
class ToolCustomConfigurationsViewSet(viewsets.ModelViewSet):
queryset = CustomConfigurations.objects.all()
parser_classes = (FormParser, MultiPartParser,)
serializer_class = CustomConfigurationsSerializers
def get_queryset(self):
queryset = self.queryset.all().order_by('-id')
return queryset
serializer.py
class CustomConfigurationsSerializers(serializers.ModelSerializer):
class Meta:
model = CustomConfigurations
fields = '__all__'
【问题讨论】:
标签: python django django-rest-framework sftp