Use Python to implement a simple image upload storage service

Source: Internet
Author: User
GitHub Address: Https://github.com/honmaple/maple-file

A simple image upload service with flask implementation

Design Intent

For the storage of pictures, there are many choices, general use of cloud services such as (Seven Bull, and Pat, etc.), but the domestic service like seven cattle
The custom domain name unexpectedly needs the domain name record (excuse me, originally is because the record trouble chooses the foreign country), and wasted my ten dollars,

And I imagine seven cows can be directly in the local can upload pictures, find out, did not find a more appropriate, so spend two days to write a

Use

Since the original intention is that the local script can be published, so there is no front-end interface, and so on later time, plus

Api

  • /api/login

      • POST

        Login
  • /api/logout

      • GET

        Cancellation
  • /api/albums

      • Name album names

      • Description Album Description

      • GET
        Get a list of albums

      • POST
        New album

  • /api/albums/<int:pk>

    PK Album ID

      • Name album names

      • Description Album Description

      • GET
        Get album information

      • PUT
        Modify album information

      • DELETE
        Delete albums

  • /api/images

      • Images uploading a picture list

      • Album album ID (a new default album is created by defaults)

      • GET
        Get a list of pictures

      • POST
        Upload image

  • /api/images/<int:pk>

      • Name picture names

      • Description Picture Description

      • GET
        Get picture information

      • PUT
        Modify picture information

      • DELETE
        Delete a picture

Configuration

Class Config (object):    DEBUG = True # production environment set to false    Secret_key = ' CCC ' # import Os;os.urandom ($)    Secret_key_ SALT = ' ssss '    json_as_ascii = False    permanent_session_lifetime = Timedelta (days=3)    per_page = 10 # pagination    Admin_url = '/admin/aaaaa ' # background address    Login_token_header = ' Api-key ' # will affect the back headers settings    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 ']) # allow upload of extension    upload_folder_root = Os.path.dirname (Os.path.abspath ( file))    Upload_folder_path = ' images ' # address relative to the current file path    Upload_folder = Os.path.join (Upload_folder_root, UPLOAD _folder_path)

Create an Account

Python manager.py Create_user

A default album will be created at the same time

Python manager.py key

Show user key

Upload

Image save path after uploading

    • Original path:

      Upload_folder + ' username ' + ' photo ' +%Y +%m + (timestamp + random number). png
    • Thumbnail path:

      Upload_folder + ' username ' + ' thumb ' +%Y +%m + (timestamp + random number). png

Example

Use requests to upload locally

Import requestsdef 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.