A better solution is to use third-party storage servers, such as Qiniu, Alibaba Cloud OSS, Amazon S3, etc. Putting all files in these storage servers can reduce the burden on the servers. Only the necessary static files and codes remain in the server.
Taking Alibaba Cloud OSS as an example, this paper explains how to use the third-party storage server. (I just used this recently. Django has a third-party library written by others. )
First, you need to have OSS. Just go to Alibaba Cloud to buy this. You can get a series of information such as keys after purchase.
Next, install the oss2 library, which is the operating library of Python corresponding to OSS.
Then install or download the repository of Django OSS. These libraries are storage classes that inherit Django and override related methods. Django's storage manages the storage of uploaded files. How to customize storage can refer to Django's official documentation.
Execute the following command to install Django-Aliyun-OSS2-Storage:
You can also install without pip, open Github of a third-party library and download the source code locally. Some code needs to be modified here, so download it directly and put the whole package in the root directory (or other location) of Django project.
After the installation and download are completed, configure the settings of Django and add the following settings:
In addition, there are two corresponding parameters to be noted, MEDIA_ROOT and MEDIA_URL.
MEDIA_ROOT is the root directory for uploading media files. Because BUCKET_NAME is set, it is usually in this BUCKET. Can be set to an empty string.
The file will naturally be uploaded to the upload_to location set in the FileField field of Django template.
MEDIA_URL is a link prefix for obtaining media files, which can be added according to the link location of oss files.
BUCKET_ACL_TYPE is set to public * * * because the uploaded file needs to be downloaded by users. If your static files also need to be uploaded to OSS, the settings are as follows:
After the settings are correct, restart Django to use. Uploaded files will be automatically uploaded to OSS.
As mentioned above, I want to modify the source code inside. Because when downloading, I found that the file name of the uploaded file is a string of garbled codes, not the file name when uploading. This requires setting some header information, please refer to the SDK documentation of OSS. You need to submit the header when uploading the file, but django-aliyun-oss2-storage above has no header information when uploading the file.
Open the source file of the package, backends.py, and find the _save method of the AliyunBaseStorage class. Revise as follows:
This setting can be downloaded by clicking the file link, and the downloaded file name is the uploaded file name. If you don't need to handle all types of files like this, you can judge the suffix of the file name to handle it.
Who knows when you feel tired and who knows when you feel lonely. Some things can only be done by yourself. The following is my funny personalit