django更改存储方式,实现连接sftp上传文件到其他服务器
下面只是简单地实现方法(demo),具体内容视自己需求具体使用
关于使用drf-yasg中的swagger实现能够上传文件的测试功能
需要在视图文件中使用解析器,为了更改上传文件的头部信息,重点在’parser_classes’ 详情,可以参考drf-yasg的git中issues,有相关内容,
安装django-storages:
? (django-storages封装了对python连接sftp的连接)
pip install django-storages
setting.py中配置SFTPStorages作为后端存储文件的方式
DEFAULT_FILE_STORAGE = 'storages.backends.sftp.SFTPStorage'
SFTP_STORAGE_HOST = '10.69.39.25'
SFTP_STORAGE_ROOT = 'home/apadmin/'
SFTP_STORAGE_PARAMS = {
'username': 'ROOT',
'password': 'ROOT',
'allow_agent': False,
'look_for_keys': False,
}
SFTP_STORAGE_INTERACTIVE = False
配置model.py
from django.db import modelsfrom storages.backends.sftpstorage import SFTPStorage
from drf_vue_blog.settings import SFTP_STORAGE_ROOT
sftp = SFTPStorage()
class Test(models.Model):
filename = models.CharField(max_length=100,verbose_name='存放文件的名字',default='')
filepath = models.FilePathField(path=SFTP_STORAGE_ROOT,default='', verbose_name='存放文件的路徑')
views.py
class TestView(APIView):
parser_classes = (MultiPartParser,)
@swagger_auto_schema(operation_description='Upload File',
operation_id='Upload File',
manual_parameters=[openapi.Parameter(name='file',
in_=openapi.IN_FORM,
type=openapi.TYPE_FILE,
required=True,
description='Upload File'),]
def post(self,request):
obj = request.FILES.get('file')
f = sftp.sftp.open(obj.name,'wb')
for i in obj.chunks():
f.write(i)
f.close()
Test.objects.create(filename=obj.name,
filepath=os.path.join(SFTP_STORAGE_ROOT,obj.name))
from django.contrib import admin
from django.urls import path, include
from article.views import TestView
urlpatterns = [
path('upload', TestView.as_view())
path('download',DownloadView.as_view())
]
class productpnserializer(serializers.Serializer):
file_name = serializers.CharField(label='下載文件的名字', required=True)
class DownloadView(APIView):
@swagger_auto_schema(query_serializer=productpnserializer,operation_summary='下載文件')
def get(self,request):
last = Test.objects.last()
print(last.filename,last.filepath)
file_name = request.query_params.get('file_name')
if not file_name:
pass
response = FileResponse(sftp.sftp.open(file_name, 'rb'))
response['Content-Type'] = 'application/octet-stream'
response["Content-Disposition"] = "attachment; filename*=UTF-8''{}".format(escape_uri_path(file_name))
return response
|