Home >Backend Development >Python Tutorial >Implement a simple image upload and storage service using Python
GitHub address: https://github.com/honmaple/maple-file
A simple picture implemented using flaskUploadService
For the storage of pictures, there are There are many choices, generally using cloud services such as (Qiniu, Youpai, etc.), but domestic services such as Qiniu
custom domain names actually require domain name registration (Excuse me, it was originally because of the trouble of registration Choosing foreign ones), and it wasted me ten dollars.
And I wanted to upload pictures directly locally like Qiniu. I searched around and couldn't find one that was more suitable, so I spent two dollars. I wrote a
because the original intention was to publish it as a local script, so there is no front-end interface. I will add
/api/login
登录
/api/logout
注销
/api/albums
pk Album ID
GET
class Config(object): DEBUG = True # 生产环境设置为False SECRET_KEY = 'ccc' # import os;os.urandom(24) SECRET_KEY_SALT = 'ssss' JSON_AS_ASCII = False PERMANENT_SESSION_LIFETIME = timedelta(days=3) PER_PAGE = 10 # 分页 ADMIN_URL = '/admin/aaaaa' # 后台地址 LOGIN_TOKEN_HEADER = 'Api-Key' # 将会影响到后面headers设置 LOGIN_TOKEN = 'api_key' #url?api_key=21312313 MIDDLEWARE = ['storage.common.middleware.CommonMiddleware'] SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db' # SQLALCHEMY_ECHO = DEBUG UPLOAD_ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg']) # 允许上传的扩展名 UPLOAD_FOLDER_ROOT = os.path.dirname(os.path.abspath(file)) UPLOAD_FOLDER_PATH = 'images' # 相对于当前文件路径的地址 UPLOAD_FOLDER = os.path.join(UPLOAD_FOLDER_ROOT, UPLOAD_FOLDER_PATH)Create account
python manager.py create_user
python manager.py keyDisplay user keyUploadUploaded image saving path Original image path:
UPLOAD_FOLDER + 'username' + 'photo' + %Y + %m + (时间戳+随机数).png
UPLOAD_FOLDER + 'username' + 'thumb' + %Y + %m + (时间戳+随机数).pngexample
import requests def images(): url = 'http://127.0.0.1:8000/api/images' files = {'images': open('desktop.png', 'rb')} multiple_files = [ ('images', ('11.png', open('11.png', 'rb'), 'image/png')), ('images', ('desktop.png', open('desktop.png', 'rb'), 'image/png')) ] headers = { 'Api-Key': 'InhpeWFuZzA4MDdJBtx4AWlPpI_Oxx1Ki8', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36' } # r = requests.post(url, files=multiple_files, headers=headers) r = requests.post(url, files=files, headers=headers) print(r.text)
The above is the detailed content of Implement a simple image upload and storage service using Python. For more information, please follow other related articles on the PHP Chinese website!