Django is an open-source web application framework based on Python, which greatly simplifies the development process of web applications. To better meet the needs of modern web applications, Django offers numerous extensions, including cloud storage.
This article mainly discusses how to use the COS plugin to implement remote attachment functionality, storing Django application data on Tencent Cloud Cloud Object Storage (COS).
Preparations
1. You have created a bucket; otherwise, create one as instructed in Creating Bucket.
2. You have created a server such as a CVM instance as instructed in Cloud Virtual Machine.
Environment Dependencies
Python version: v3.8 or later.
Django version: Greater than or equal to 2.2, and less than 3.3.
Practical Steps
Creating COS bucket
1. Create a bucket with public-read/private-write access permissions. It is recommended that the bucket's region matches the region of the CVM running Django. For more information on creating a bucket, please refer to the Creating Bucket documentation.
2. Locate the newly created bucket in the bucket list and obtain the bucket name, such as examplebucket-1250000000.
Create Django Project
1. Visit the PyCharm Official Website and select the appropriate PyCharm version for your operating system.
2. After installing and opening PyCharm, click on NEW project or create project, and select Django from the options.
3. After creating the bucket, locate and open the setting.py file in your directory.
4. Copy and paste the following code, and configure the COS service according to the parameter descriptions:
The name customized during bucket creation such as examplebucket-1250000000.
Region
The region selected during bucket creation
SecretId
Access key information can be created and obtained in Cloud API Key. It is recommended to use a sub-account key and follow the principle of least privilege to reduce risks. For more information, see Sub-account Access Key Management.
SecretKey
Access key information can be created and obtained in Cloud API Key. It is recommended to use a sub-account key and follow the principle of least privilege to reduce risks. For more information, see Sub-account Access Key Management.
Download and Configure COS Plugin
1. Visit Github to download the COS plugin. After downloading, extract the django_cos_storage directory into your Django project directory.
Note
To view the plugin information, open the terminal and enter pip freeze to display the module information.
2. Create a Python file in the django_cos_storage directory, for example, COSStorage.py.
In this example, cessu/1.png represents creating a folder named cessu and uploading the image 1.png to the cessu folder. After a successful upload, you can find the image 1.png in the cessu folder of the COS bucket.
4. Locate and open the urls.py file in the djangoProject2 directory.
Copy and paste the code:
from django.contrib import admin
from django.urls import path
from app_cos.views import *
urlpatterns =[
path('admin/', admin.site.urls),
path('upload_file/', upload_file_view),
]
5. In the terminal, enter python manage.py createsuperuser and follow the prompts to enter the username and password.
6. Then, enter python .\manage.py runserver in the terminal to run.
7. Open the website http://127.0.0.1:8000/admin/ and enter the account credentials you just set to log in.
Note
If the website displays an error message as follows:
Return to PyCharm, reopen the terminal, and enter the following commands sequentially:
python manage.py makemigrations
python manage.py migrate
Finally, enter python .\manage.py runserver in the terminal to run the server, and then open http://127.0.0.1:8000/admin/ to access the application.
Verify Django Attachment Storage in COS
1. Visit http://127.0.0.1:8000/upload_file to complete the file upload process. When prompted as shown below, the upload is successful.
2. Log in to the COS console, select the previously created bucket, and you can view the uploaded images under the cessu path.
Summary
Of course, COS not only offers the above applications and services but also supports various popular open-source applications integrated with Tencent Cloud COS plugins. Click here to launch and start using them immediately!