JSON 기반의 메시지가 왔다 갔다는 아무런 문제가 없는데,
파일 업로드 혹은 이미지 바이너리를 업로드 하거나 다운로드 해야 한다면 어떻게 전달해 줘야 하나?
Form-data 로 파일 받기
일단 상식처럼 HTTP form-data 로 처리하는 것으로 되어 있는데 (JSON에 그냥 바이너리를 넣으면 안되나?)
파일 사이즈도 그렇고 처리하는 기본적인 방법을 알아놔야 한다.
일단 레퍼런스 페이지 부터 조사해서 남겨두자. 아래에 정리해둠.
DB 필드로 파일을 저장하는 방법
그리고, 들어온 파일 바이너리를 MongoDB에 저장해야 하는데 이건 또 다른 영역이군.
FileField 가 따로 있는 것인지?
from mongoengine import FileField
# If you are using FileField in a http framework you will need StringIO
# as a replacement for the object file or you can use TemporaryFile
mem_file = StringIO()
mem_file.write(image_content_from_http)
Object.upload.put(mem_file, content_type='image/png')
여기도 살펴봐야겠네.
https://docs.mongoengine.org/guide/gridfs.html
2.7. GridFS — MongoEngine 0.23.1 documentation
Warning The FileField in a Document actually only stores the ID of a file in a separate GridFS collection. This means that deleting a document with a defined FileField does not actually delete the file. You must be careful to delete any files in a Document
docs.mongoengine.org
유용한 참고 글들
https://malwareanalysis.tistory.com/89
Flask restful API 파일 업로드 구현
Flask restful API모듈을 사용하여 파일 업로드를 간단하게 구현하는 방법을 소개합니다. 영상: https://youtu.be/nHxlRA8TrUo ▶노트북 사양과 녹화 환경이 좋지 않아 화질이 다소 낮습니다. 1. 준비 2가지
malwareanalysis.tistory.com
Postman에서 form-data 필드에 파일 넣어서 보내는 방법이 있네, 그림으로 알아두자. 출처는 바로 위 링크입니다.
Flask-restplus 파일/이미지 업로드하는 방법
Flask로 Restful API를 만들기위해 라이브러리를 찾아보면 flask-restful, flask-restplus등 다양한 라이브러리가 존재한다. 각 라이브러리마다 사용법 또한 다르며 장점, 단점도 다르기 때문에 document를 잘
www.hides.kr
키워드 라이브러리, 검색을 위해서는 단어가 필요한데 잘 알아두자.
from werkzeug.datastructures import FileStorage
from werkzeug.utils import secure_filename
# From file uploads
parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files')
https://jaehaaheaj.tistory.com/entry/flask-003
[flask] 파일 받아와서 저장하기 - 1
https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/ Uploading Files — Flask Documentation (1.1.x) Uploading Files Ah yes, the good old problem of file uploads. The basic idea of file u..
jaehaaheaj.tistory.com
BJ.
Markus Spiske 님의 사진, 출처: Pexels
'IT > 파이썬' 카테고리의 다른 글
파이썬 동작 로그 파일 저장하기 - flask gunicorn logging (0) | 2021.07.27 |
---|---|
윈도우 flask gunicorn 에러 발생 시 대안 - No module named 'fcntl' (0) | 2021.07.27 |
Flask JWT 로그인 기능을 위한 자료 조사 - flask-jwt-extended (0) | 2021.07.01 |
작업중 - 서버 구조 구성 Flask API + ReactJS Frontend + GUnicorn (0) | 2021.06.30 |
JWT access token, refresh token 용도 (0) | 2021.06.30 |