from datetime import datetime
from http import HTTPStatus
from os import access
from flask import request
from flask_jwt_extended import create_access_token, get_jwt, get_jwt_identity, jwt_required
from flask_restful import Resource
from mysql.connector.errors import Error
from mysql_connection import get_connection
import mysql.connector
from config import Config
import boto3
pip install boto3로 설치하고 import 한다. boto3는 aws를 파이썬에서 연결할 수 있도록 해주는 라이브러리다.
class PostingResource(Resource):
def post(self):
# 1. 클라이언트로부터 데이터 받아온다.
# photo(file), content(text)
if 'photo' not in request.files:
return {'error', '파일을 업로드하세요'}, 400
file = request.files['photo']
content = request.form['content']
# 2. S3에 파일 업로드
# 파일명을 우리가 변경해 준다.
# 파일명은, 유니크하게 만들어야 한다.
current_time = datetime.now()
new_file_name = current_time.isoformat().replace(':', '_') + '.jpg'
# 유저가 올린 파일의 이름을, 내가 만든 파일명으로 변경
file.filename = new_file_name
# S3 에 업로드 하면 된다.
# AWS 의 라이브러리를 사용해야 한다.
# 이 파이썬 라이브러리를 boto3 라이브러리다!
# boto3 라이브러리 설치
# pip install boto3
s3 = boto3.client('s3', aws_access_key_id = Config.ACCESS_KEY, aws_secret_access_key = Config.SECRET_ACCESS)
try:
s3.upload_fileobj(file, Config.S3_BUCKET, file.filename, ExtraArgs = {'ACL':'public-read', 'ContentType':file.content_type})
except Exception as e:
return {'error':str(e)}, 500
# 3. DB에 저장
try :
# 데이터 insert
# 1. DB에 연결
connection = get_connection()
# 2. 쿼리문 만들기
query = '''insert into posting
(content, imgUrl)
values
( %s, %s);'''
record = (content, new_file_name)
# 3. 커서를 가져온다.
cursor = connection.cursor()
# 4. 쿼리문을 커서를 이용해서 실행한다.
cursor.execute(query, record)
# 5. 커넥션을 커밋해줘야 한다 => 디비에 영구적으로 반영하라는 뜻
connection.commit()
# 6. 자원 해제
cursor.close()
connection.close()
except mysql.connector.Error as e :
print(e)
cursor.close()
connection.close()
return {"error" : str(e)}, 503
return {'result':'success'}
포스트맨에 url 입력하고 경로 연결해준다음
클라이언트로부터 데이터를 받아온다.
boto3.client로 연결하고 upload_fileobj로 파일 업로드를 한다.
mysql db에는 글 내용과 파일이름을 업로드한다.
읽어올때는 s3주소뒤에 파일이름을 붙이면 파일을 읽어올 수 있다.
'REST API' 카테고리의 다른 글
boto3로 Rekognition 사용하기 (0) | 2022.06.24 |
---|---|
Flask JWT 토큰 로그아웃 하기 (0) | 2022.06.21 |
Flask 토큰 유효기간 만료 시키는 방법 (0) | 2022.06.20 |
Flask에서 로그인한 유저만 처리할 수 있는 API에 토큰 적용하는 방법 (0) | 2022.06.20 |
Flask 회원가입/로그인 API에서, 토큰 생성해서 처리하는 방법 (0) | 2022.06.20 |