Release Notes
Announcements


DEFAULT_FILE_STORAGE = "django_cos_storage.TencentCOSStorage"TENCENTCOS_STORAGE = {"BUCKET": "xxx","CONFIG": {"Region": "ap-guangzhou","SecretId": "xxxx","SecretKey": "xxxx",}}
Configuration Item | Configuration Value |
Bucket | The custom name defined when a bucket is created, for example, examplebucket-1250000000. |
Region | The region selected when the bucket was created. |
SecretId | Access key information can be created and obtained in TencentCloud API Keys. It is recommended to use sub-account keys and follow the principle of least privilege to mitigate usage risks. For details, see Access Key Management for Sub-accounts. |
SecretKey | Access key information can be created and obtained in TencentCloud API Keys. It is recommended to use sub-account keys and follow the principle of least privilege to mitigate usage risks. For details, see Sub-account Access Key Management. |
pip freeze, then you can view its module information.
from .storage import TencentCOSStoragefrom functools import wrapsdef decorator(cls):instance = None@wraps(cls)def inner(*args,**kwargs):nonlocal instanceif not instance:instance = cls(*args,**kwargs)return instancereturn inner@decoratorclass QFStorage:def __init__(self):passself.storage =TencentCOSStorage()self.bucket =self.storage.bucketself.client =self.storage.client#Upload Objectdef upload_file(self, Key, LocalFilePath, PartSize=1, MAXThread=5, EnableMD5=False):try:response =self.client.upload_file(Bucket=self.bucket,Key=Key,LocalFilePath=LocalFilePath,PartSize=PartSize,MAXThread=MAXThread,EnableMD5=EnableMD5)return responseexcept Exception as e:print('Failed to upload object, error:', e)return None

from django.shortcuts import render,redirectfrom django.http import HttpResponsefrom django_cos_storage.COSStorage import QFStoragefrom django.conf import settings#Upload Objectdef upload_file_view(request):response = QFStorage().upload_file(Key='1.png',LocalFilePath=settings.BASE_DIR / 'cessu/1.png')if response:return HttpResponse('File uploaded successfully!')return HttpResponse('File upload failed')
cessu/1.png represents the local file to be uploaded, 1.png is located in the cessu folder under the project directory. After successful upload, you can find the image 1.png in the cessu folder of the COS bucket.
from django.contrib import adminfrom django.urls import pathfrom app_cos.views import *urlpatterns = [path('admin/', admin.site.urls),path('upload_file/', upload_file_view),]
python manage.py migrate in the terminal and run it.python manage.py createsuperuser in the terminal and follow the prompts to enter your username and password.python manage.py createsuperuser prompts that pkg_resources is missing, please execute the installation command pip install setuptools to resolve it.
python .\\manage.py runserver in the terminal.
http://127.0.0.1:8000/admin/ and enter the previously set username and password to log in.

python manage.py makemigrationspython manage.py migrate
python .\\manage.py runserver in the terminal, then open http://127.0.0.1:8000/admin/.http://127.0.0.1:8000/upload_file to complete the operation to upload files. When the prompt as shown below is displayed, it indicates a successful upload.
Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
masukan