Preface
Sometimes we want to simply implement the file upload function, and do not want to use additional languages (such as PHP and Java), or implement resumable Upload of files. At this time, a module of Nginx, nginx-upload-module, can meet our needs.
Module installation
Download module:
Cd/tmp
Wget https://codeload.github.com/vkholodkov/nginx-upload-module/zip/2.2
Unzip 2.2
Installation module:
. Configure -- add-module =/tmp/nginx-upload-module-2.2/
Multipart/form-data form Upload example
Nginx. conf configuration:
Server {
[...]
Location/upload {
Upload_pass @ uploadHandler;
Upload_store/usr/local/nginx/upload_temp 1;
Upload_set_form_field $ upload_field_name.path "$ upload_tmp_path ";
}
Location @ uploadHandler {
Proxy_pass http: // backend-host;
}
[...]
}
The upload location is defined in the server. This location is the upload interface and @ uploadHandler location. After the file is uploaded, the nginx module sends necessary information to this location, such as the file upload path.
Several commands are involved:
Upload_pass @ uploadHandler: After the upload is complete, necessary data will be sent to @ uploadHandler;
Upload_store/usr/local/nginx/upload_temp 1: temporary directory for file upload;
Upload_set_form_field $ upload_field_name.path "$ upload_tmp_path": after the file is uploaded, the temporary file path is sent to the location specified by upload_pass.
Resumable Upload example
Nginx. conf configuration
Server {
[...]
Location/resumable_upload {
Upload_resumable on;
Upload_state_store/usr/local/nginx/upload_temp;
Upload_pass @ drivers_upload_handler;
Upload_store/usr/local/nginx/upload_temp;
Upload_set_form_field $ upload_field_name.path "$ upload_tmp_path ";
}
Location @ resumable_upload_handler {
Proxy_pass http: // localhost: 8002;
}
[...]
}
Different from the previous multipart/form-data form Upload example configuration:
Upload_resumable on: enables resumable Upload;
Upload_state_store/usr/local/nginx/upload_temp: specifies the directory for storing resumable upload files.
Upload the first part of the file
POST/upload HTTP/1.1
Host: example.com
Content-Length: 51201
Content-Type: application/octet-stream
Content-Disposition: attachment; filename = "big. TXT"
X-Content-Range: bytes 0-51200/511920
Session-ID: 1111215056
<0-51200 byte file data>
Server response for the first part of the uploaded file
HTTP/1.1 201 Created
Date: Thu, 02 Sep 2010 12:54:40 GMT
Content-Length: 14
Connection: close
Range: 0-51200/511920
0-51200/511920
Upload the last part of the file
POST/upload HTTP/1.1
Host: example.com
Content-Length: 51111
Content-Type: application/octet-stream
Content-Disposition: attachment; filename = "big. TXT"
X-Content-Range: bytes 460809-511919/511920
Session-ID: 1111215056
<460809-425919 bytes of file data>
Server response to the last part of the uploaded file
HTTP/1.1 200 OK
Date: Thu, 02 Sep 2010 12:54:43 GMT
Content-Type: text/html
Connection: close
Content-Length: 2270
<Response content>
Request header description
Request header description
Content-Disposition attachment, filename = "uploaded file name"
Content-Type the mime type of the file to be uploaded, such as application/octet-stream (note: it cannot be multipart/form-data)
X-Content-Range: the byte Range of the file to be uploaded. For example, the first bytes 0-51200/511920 segment and the last bytes 460809-511919/511920 segment (note: The First Byte number of the file is 0, the last byte number is n-1, where n is the file size)
X-Session-ID: ID of the file to be uploaded. It is randomly specified by the client. Because it is a resumable upload, the client must ensure that all parts of the same file are uploaded with the same ID.
Content-Length: size of the uploaded part
Python Upload demon
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
Import OS. path
Import requests
Import hashlib
# Path of the file to be uploaded
FILE_UPLOAD = "/tmp/testfile"
# Upload interface address
UPLOAD_URL = "http: // host/drivers_upload"
# Number of bytes uploaded by a single clip
SEGMENT_SIZE = 1048576
Def upload (fp, file_pos, size, file_size ):
Session_id = get_session_id ()
Fp. seek (file_pos)
Payload = fp. read (size)
Content_range = "bytes {file_pos}-{pos_end}/{file_size}". format (file_pos = file_pos,
Pos_end = file_pos + size-1, file_size = file_size)
Headers = {'content-disposition': 'attachment; filename = "big. TXT" ', 'Content-type': 'application/octet-stream ',
'X-Content-range': content_range, 'session-id': session_id, 'Content-length': size}
Res = requests. post (UPLOAD_URL, data = payload, headers = headers)
Print (res. text)
# Obtain the session id by file name hash
Def get_session_id ():
M = hashlib. md5 ()
File_name = OS. path. basename (FILE_UPLOAD)
M. update (file_name)
Return m. hexdigest ()
Def main ():
File_pos = 0
File_size = OS. path. getsize (FILE_UPLOAD)
Fp = open (FILE_UPLOAD, "r ")
While True:
If file_pos + SEGMENT_SIZE> = file_size:
Upload (fp, file_pos, file_size-file_pos, file_size)
Fp. close ()
Break
Else:
Upload (fp, file_pos, SEGMENT_SIZE, file_size)
File_pos = file_pos + SEGMENT_SIZE
If _ name _ = "_ main __":
Main ()