Home > Backend Development > Python Tutorial > Implement a simple image upload and storage service using Python

Implement a simple image upload and storage service using Python

高洛峰
Release: 2017-03-16 15:51:50
Original
2699 people have browsed it

GitHub address: https://github.com/honmaple/maple-file

A simple picture implemented using flaskUploadService

Original design intention

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

using

because the original intention was to publish it as a local script, so there is no front-end interface. I will add

when I have time. API

  • /api/login

    • ##POST

      登录
      Copy after login
  • /api/logout

    • GET

      注销
      Copy after login
  • /api/albums

    • ##name Album name
    • description Album description
    • GET
    • Get the album list


    • POST
    • New album


  • /api /albums/

    pk Album ID

    • name Album name
    • description Album description
    • GET
    • Get album information


    • PUT
    • Modify album information


    • DELETE

      Delete
      Album

    ##/api/images
  • images Upload image list
    • album Album ID (a new
    • default
    • album will be created by default)

      GET
    • Get the picture list

    • POST
    • Upload pictures

    /api/images/
  • name Image name
    • description Image description
    • GET
    • Get picture information

    • PUT
    • Modify picture information

    • DELETE
    • Delete pictures

    • Configuration
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)
Copy after login
Create account

python manager.py create_user
Copy after login

will also create a

default

photo album

python manager.py key
Copy after login
Display user keyUpload

Uploaded image saving path

Original image path:
    UPLOAD_FOLDER + 'username' + 'photo' + %Y + %m + (时间戳+随机数).png
    Copy after login
  • Thumbnail
  • Path:

    UPLOAD_FOLDER + 'username' + 'thumb' + %Y + %m + (时间戳+随机数).png
    Copy after login
    example
  • Use

    requests

    Local upload

    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)
    Copy after login
                                                                     


    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!

    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template