<AWS>s3 파일업로드
앞부분다시 정리하기!
s3 setting
"CORS configuration" 버튼을 눌러서 CORS 설정을 해주도록 하자.
위에 설명은 XML형식이므로 JSON형식으로 바꿔서 설정해주기
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": [
"ETag"
],
"MaxAgeSeconds": 3000
}
]
Django에서 boto3를 사용하여 S3와 연결 및 파일 업로드
터미널에서 boto3설치하기
$ pip install boto3
# django 앱 views.py
import boto3
from django.http import HttpResponse
import해주기!
class FileView(View):
s3_client = boto3.client(
's3',
aws_access_key_id="s3세팅 시 설치한new_user_credentials에 있는 키ID",
aws_secret_access_key="s3세팅 시 설치한new_user_credentials에 있는 키"
)
def post(self, request):
file = request.FILES['filename'] #['키값']
self.s3_client.upload_fileobj(
file,
"wecode16", #버킷이름
file.name,
ExtraArgs={
"ContentType": file.content_type
}
)
return HttpResponse(status= 200)
url 설정
from users.views import FileView
from <앱이름>.views import FileView
urlpatterns = [
path("/file", FileView.as_view()),
]
postman 이용 시
HTTPie 이용 시
http -f POST 127.0.0.1:8000/user/file filename@~/Desktop/database.png
http -f POST <ip 주소>/<url> <키값>@~/<업로드할 파일경로와 이름>
Author And Source
이 문제에 관하여(<AWS>s3 파일업로드), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@mini_y/AWSs3-파일업로드저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)