如何在DigitalOcean部署的Django项目中定期备份并提交用户上传文件?
高效处理Django项目中上传文档的方案
方案1:批量下载+自动提交仓库
第一步:实现一键批量下载接口
在Django中新增视图,遍历所有用户上传的xlsx/csv文件,打包成zip后提供批量下载,仅对管理员开放访问。
示例代码(假设文件模型为UploadedFile,file字段存储文件路径):
from django.http import HttpResponse from django.views.decorators.http import require_GET import zipfile import os from io import BytesIO from myapp.models import UploadedFile @require_GET def bulk_download(request): if not request.user.is_staff: return HttpResponse(status=403) zip_buffer = BytesIO() with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file: for uploaded_file in UploadedFile.objects.all(): file_path = uploaded_file.file.path zip_file.write(file_path, os.path.basename(file_path)) zip_buffer.seek(0) response = HttpResponse(zip_buffer, content_type='application/zip') response['Content-Disposition'] = 'attachment; filename="all_uploads.zip"' return response
将该视图配置到urls.py后,管理员即可通过对应链接一键下载所有文件。
第二步:自动同步到代码仓库
编写Django管理命令,配合服务器定时任务实现自动同步、提交:
- 创建管理命令文件
management/commands/sync_uploads_to_repo.py:
from django.core.management.base import BaseCommand import os import subprocess from myapp.models import UploadedFile class Command(BaseCommand): help = '自动同步上传文件到git仓库' def handle(self, *args, **options): repo_files_dir = '/path/to/your/repo/uploaded_files' if not os.path.exists(repo_files_dir): os.makedirs(repo_files_dir) # 复制所有上传文件到仓库目录 for uploaded_file in UploadedFile.objects.all(): src_path = uploaded_file.file.path dest_path = os.path.join(repo_files_dir, os.path.basename(src_path)) with open(src_path, 'rb') as src, open(dest_path, 'wb') as dest: dest.write(src.read()) # 执行git提交命令 subprocess.run(['git', 'add', 'uploaded_files/'], cwd='/path/to/your/repo') subprocess.run(['git', 'commit', '-m', 'Auto sync uploaded files'], cwd='/path/to/your/repo') subprocess.run(['git', 'push'], cwd='/path/to/your/repo')
- 在DigitalOcean服务器设置cron定时任务(比如每天凌晨执行):
0 0 * * * /path/to/your/venv/bin/python /path/to/your/project/manage.py sync_uploads_to_repo
无需手动操作,即可自动完成文件同步与仓库提交,触发DigitalOcean的部署更新。
方案2:实时同步上传文件到仓库
如果需要用户上传文件后立即同步到仓库,可利用Django的模型信号实现:
from django.db.models.signals import post_save from django.dispatch import receiver from myapp.models import UploadedFile import subprocess import os @receiver(post_save, sender=UploadedFile) def sync_file_to_repo(sender, instance, created, **kwargs): if created: repo_files_dir = '/path/to/your/repo/uploaded_files' if not os.path.exists(repo_files_dir): os.makedirs(repo_files_dir) src_path = instance.file.path dest_path = os.path.join(repo_files_dir, os.path.basename(src_path)) with open(src_path, 'rb') as src, open(dest_path, 'wb') as dest: dest.write(src.read()) # 提交新文件到git subprocess.run(['git', 'add', dest_path], cwd='/path/to/your/repo') subprocess.run(['git', 'commit', '-m', f'Add new file: {os.path.basename(src_path)}'], cwd='/path/to/your/repo') subprocess.run(['git', 'push'], cwd='/path/to/your/repo')
用户上传文件后,系统会自动同步到仓库并触发部署更新。
方案3:优化存储到DigitalOcean Spaces(可选)
若文件数量较多,建议将上传文件存储到DigitalOcean Spaces对象存储:
- 可直接通过Spaces控制台批量下载所有文件
- 用
s3cmd工具一键同步文件到本地或仓库:
s3cmd sync s3://your-spaces-bucket/uploaded_files/ /path/to/your/repo/uploaded_files/
配合cron定时执行,或利用Spaces生命周期规则自动备份,进一步简化流程。
内容的提问来源于stack exchange,提问作者Luiz2002
相关产品推荐
相关产品推荐

