Skip to content

Rest API upload file with Python Django ๐Ÿก ๐Ÿ†

Notifications You must be signed in to change notification settings

mrneodiablo/api-upload-files

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

38 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

HTTP API UPLOAD FILE

Features

  • upload file
  • retrieve file by name
  • delete file by name
  • if multiple files have similar contents, reuse the contents
  • authentication api with Basic Authen
  • signature in http header to make secure data in transport
  • check file type in white-list
  • check size limit

Detail

  1. multiple files have similar contents, reuse the contents

I will use database for save real name of file, file content will be save on disk and rename with its content after md5

==> reuse contents decreases disk use for save data file

Example

Database

|------------|---------------|
|  name      |  hash_content |
|------------|---------------|
|a.png       |  abcdd        |
|b.png       |  abcdd        |
|c.pgn       |  adssadds     |
|------------|---------------|

File

file on disk will be rename build hash content file

abcdd
adssadds
  1. Authen Basic

==> Authen API

authen_basic = base64.b64encode(username + ":" + passwd).decode("ascii")

headers = {
            'authenticate': 'Basic ' + authen_basic,
}
  1. make secure data in transport

Client will generate sign and put it in header , after that server recive data will be generate sign and recheck with header client send to

==> make secure data in transport

    SECRET_KEY = "sdfsfFKLJodsg082343223_"
    sign = hash_md5(str(md5_content_file + SECRET_KEY))
    
    headers = {
                'authenticate': 'Basic ' + str(generator_authen_basic()),
                'sign-client': sign)
    }
  1. Check file type in white-list
FILE_TYPE_ALLOW = {
    "png": "image/png",
    "jpeg": "image/jpeg",
    "jpg": "image/jpeg",
    "pdf": "application/pdf",
    "txt": "text/plain",
    "csv": "text/plain",
    "abc": "application/octet-stream"
}

  1. Check file size limit
FILE_SIZE = 1024*1024 # 1 MB

Use HTTP REST API

GET FILE
  • end point: http://xxx/api/v1/file/<filename>
  • method: GET
  • authentication: header
UPLOAD FILE
  • end point: http://xxx/api/v1/file/
  • method: POST
  • authentication: header
UPDATE FILE
  • end point: http://xxx/api/v1/file/<filename>
  • method: PUT
  • authentication: header
DELETE FILE
  • end point: http://xxx/api/v1/file/<filename>
  • method: DELETE
  • authentication: header

Setup

  • install manual with CentOS 6/7
  • install manual with NixOs
  • install by Dockerfile
  • install by docker-compose

Detail for setup

Test API

Detail for test

About

Rest API upload file with Python Django ๐Ÿก ๐Ÿ†

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published