릴리스 노트
제품 공지


DEFAULT_FILE_STORAGE = "django_cos_storage.TencentCOSStorage"TENCENTCOS_STORAGE = {"BUCKET": "xxx","CONFIG": {"Region": "ap-guangzhou","SecretId": "xxxx","SecretKey": "xxxx",}}
구성 항목 | 구성 값 |
Bucket | 버킷 생성 시 사용자 정의한 이름, 예: examplebucket-1250000000. |
Region | 버킷 생성 시 선택한 리전. |
SecretId | 액세스 키(Access Key) 정보, API 키에서 생성 및 가져올 수 있습니다. 서브 계정 키를 사용하여 최소 권한 원칙에 따른 권한 부여로 사용 위험을 줄이는것을 권장합니다. 상세한 내용은 서브 계정 액세스 키 관리를 참고하십시오. |
SecretKey | 액세스 키(Access Key) 정보, API 키에서 생성 및 가져올 수 있습니다. 서브 계정 키를 사용하여 최소 권한 원칙에 따른 권한 부여로 사용 위험을 줄이는것을 권장합니다. 상세한 내용은 서브 계정 액세스 키 관리를 참고하십시오. |
pip freeze를 입력하면 해당 모듈 정보를 확인할 수 있습니다.
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#객체 업로드def 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('객체 업로드 실패, error:', e)return None

from django.shortcuts import render,redirectfrom django.http import HttpResponsefrom django_cos_storage.COSStorage import QFStoragefrom django.conf import settings#객체 업로드def upload_file_view(request):response = QFStorage().upload_file(Key='1.png',LocalFilePath=settings.BASE_DIR / 'cessu/1.png')if response:return HttpResponse('파일 업로드 성공!')return HttpResponse('파일 업로드 실패')
cessu/1.png는 업로드할 로컬 파일 1.png가 프로젝트 디렉터리의 cessu 폴더에 위치함을 의미합니다. 업로드 성공 후, COS 버킷의 cessu 폴더에서 이미지 1.png를 찾을 수 있습니다.
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를 입력한 후 실행합니다.python manage.py createsuperuser를 입력하고, 안내에 따라 계정과 비밀번호를 입력하면 됩니다.python manage.py createsuperuser 실행 시 pkg_resources가 없다는 오류가 발생한다면, pip install setuptools 설치 명령어를 실행하여 해결할 수 있습니다.
python .\\manage.py runserver를 입력하여 실행합니다.
http://127.0.0.1:8000/admin/ 사이트를 열고 방금 설정한 계정과 비밀번호를 입력하면 로그인을 완료할 수 있습니다.

python manage.py makemigrationspython manage.py migrate
python .\\manage.py runserver를 입력하여 실행하고 http://127.0.0.1:8000/admin/을 열면 됩니다.http://127.0.0.1:8000/upload_file에 접속하여 파일 업로드 작업을 완료합니다. 다음 그림과 같은 메시지가 표시되었다면 업로드가 성공한 것입니다.
피드백