tornado+nginx上传视频文件

[http://arloz.me/tornado/2014/06/27/uploadvideotornado.html]

[NGINX REFRER:Nginx upload module]

由于tornado通过表达上传的数据最大限制在100M,所以如果需要上传视屏文件的情况在需要通过其他方式实现, 此处采用nginx的nginx-upload-modulejQuery-File-Upload实现。

1.编译安装nginx-upload-module

  • 下载nginx-1.5.8
  • 下载nginx-upload-module2.0
  • 由于nginx-upload-module不支持最新版的nginx,直接编译会出错,需要打补丁 davromaniak.txt
tar xzf nginx-1.5.8
tar xzf nginx_upload_module-2.0.12.tar.gz
cd nginx_upload_moule-2.0.12
patch ngx_http_upload_module.c davromaniak.txt
cd ../nginx-1.5.8
./configure --add-module=../nginx_upload_moule-2.0.12
make & sudo make install

2.配置nginx的upload-module

upstream tornadoserver{
    server 127.0.0.1:8001;
}
server {
listen       8080;
server_name  localhost;
location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_pass http://tornadoserver;
}
client_max_body_size 4G;
client_body_buffer_size 1024k;

if ($host !~* ^(localhost) ) {
}
location = /uploads {
    if ($request_method = OPTIONS) {
        add_header Pragma no-cache;
        add_header X-Content-Type-Options nosniff;

        # Access control for CORS
        add_header Access-Control-Allow-Origin "http://localhost";
        add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
        add_header Access-Control-Allow-Headers "cache-control, content-range, accept,            origin, session-id, content-disposition, x-requested-with, ctent-type,            content-description, referer, user-agent";
        add_header Access-Control-Allow-Credentials "true";
        # 10 minute pre-flight approval
        add_header Access-Control-Max-Age 600;
        return 204;
    }
    if ($request_method = POST) {
        add_header Pragma no-cache;
        add_header X-Content-Type-Options nosniff;
        #add_header Cache-control "no-story, no-cache, must-revalidate";

        # Access control for CORS
        add_header Access-Control-Allow-Origin "http://localhost";
        add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
        add_header Access-Control-Allow-Headers "cache-control, content-range, accept,                origin, session-id, content-disposition, x-requested-with,                content-type, content-description, referer, user-agent";
        add_header Access-Control-Allow-Credentials "true";
        # 10 minute pre-flight approval
        add_header Access-Control-Max-Age 600;

        upload_set_form_field $upload_field_name.name "$upload_file_name";
        upload_set_form_field $upload_field_name.content_type "$upload_content_type";
        upload_set_form_field $upload_field_name.path "$upload_tmp_path";

        upload_pass_form_field "^X-Progress-ID$|^authenticity_token$";
        upload_cleanup 400 404 499 500-505;
    }

    upload_pass @fast_upload_endpoint;

    # {a..z} not usefull when use zsh
    # mkdir {1..9} {a..z} {A..Z}
    upload_store /tmp/uploads 1;

    # set permissions on the uploaded files
    upload_store_access user:rw group:rw all:r;
}
location @fast_upload_endpoint {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass_header ‘Access-Control-Allow-Origin‘;
    proxy_pass http://tornadoserver;
}
}

3.demo页面

<!DOCTYPE HTML>
<html>
<head> <meta charset="utf-8">
    <title>jQuery File Upload Example</title>
    <style>
    .bar {
        height: 18px;
        background: green;
    }
</style></head>
<body>
    <input id="fileupload" type="file" name="files[]" data-url="uploads" multiple>
    <script src=""></script>
    <script src="/static/js/vendor/jquery.ui.widget.js"></script>
    <script src="/static/js/jquery.iframe-transport.js"></script>
    <script src="/static/js/jquery.fileupload.js"></script>
    <script>
        $(function () {
            $(‘#fileupload‘).fileupload({
                dataType: ‘json‘,
                done: function (e, data) {
                    $.each(data.result.files, function (index, file) {
                       $(‘<p/>‘).text(file.name+"  "+file.size).appendTo(document.body);
                    });
                },
                progressall: function (e, data) {
                    var progress = parseInt(data.loaded / data.total * 100, 10);
                    $(‘#progress .bar‘).css( ‘width‘, progress + ‘%‘);
                }
            });
        });
    </script>
    <div id="progress">
        <div class="bar" style="width: 10%;"></div>
    </div>
</body>
</html>

4.tornado处理

当nginx的upload-module完成视频文件的传输之后,其会设置表单数据,并转发给后台tornado服务器处理。

通过如下方式获得相关参数:

name = self.get_argument("files[].name","")
content_type = self.get_argument("files[].content_type","")
oldpath = self.get_argument("files[].path","")
size = os.path.getsize(oldpath)
files = []
files.append({"name":name,"type":content_type,"size":size})
ret = {"files":files}
self.write(tornado.escape.json_encode(ret))
时间: 2024-09-28 22:19:17

tornado+nginx上传视频文件的相关文章

腾讯云视频jquery批量上传视频文件

{extend name="public/layout" /} {block name="head"} <script src="//cdn-go.cn/cdn/vod-js-sdk-v6/latest/vod-js-sdk-v6.js"></script> {/block} {block name="body"} <body> <div class="p-15"&

如何解决nginx上传大文件的限制

通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题: 用nginx来做webserver的时,上传大文件时需要特别注意client_max_body_size这个参数,否则会中断在nginx的请求中,在php中是无法记录到访问的. 一般上传大文件流程: 首先修改php.ini文件: file_uploads on 是否允许通过HTTP上传文件的开关.默认为ON即是开 upload_tmp_dir – 文件上传至服务器上存储临时文件的地方,如果没指定

DJANGO技巧两则:模拟MKDIR -P及配合NGINX上传大文件不使超时

这都是在开发当哪遇到的问题,网上转转,作个记录: http://blog.chinaunix.net/uid-25525723-id-1596574.html http://bookshadow.com/weblog/2014/10/02/python-mkdir-p/ def mkdir_p(path): try: os.makedirs(path) except OSError as exc: # Python >2.5 (except OSError, exc: for Python <

使用HttpUtils 上传视频文件

private void shangchuan(){                 //文件的路径        //File file=new File(path);        File file=new File(Environment.getExternalStorageDirectory()+"/dd.mp4");        //File file=new File(Environment.getExternalStorageDirectory()+"/&q

Nginx 上传大文件时报404错误

处理方法:在配置文件中更改以下2个参数 client_max_body_size 10M; client_body_buffer_size 10M; 原文地址:http://blog.51cto.com/linux10000/2351248

使用resumable.js上传大文件(视频)兵转换flv格式

前台代码 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Video.aspx.cs" Inherits="BPMS.WEB.Video" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head

上传视频时自动完成截取缩略图(二)

从上篇文章中我们知道了ffmpeg是怎么使用的.那么这篇文章给大家介绍下我是怎么通过调用ffmpeg实现在视频上传的同时自动截取图片的. 首先我们不能直接调用ffmpeg实现想要的功能是防止cmd命令执行时出现的黑窗口.所以我们可以封装一个类,然后调用里面的方法只需传递参数就可以实现功能了. 这里我写了一个VideoConverToImg类: public class VideoConverToImg { /// <summary> /// 从视频中截取img格式图片 /// </sum

七牛上传视频添加到队列时视频大小是否添加

最近在做视频分析的项目,遇到了一些关于七牛上传视频文件的问题. 当 选择的视频文件 大于 本地剩余空间 时,则取消上传. 'FilesAdded': function(up, files) { plupload.each(files, function(file,index) { //重点在这里 var fileSize=0; for(var i=0;i<files.length;i++){ fileSize+= Math.ceil(files[i].size/1024/1024); }; //

关于安卓手机通过调用摄像头上传视频文件失败的情况

在安卓手机上用 <input type="file" id="file" onchange="filesize(this)" name="video" accept="video/*" capture="camcorder" multiple>上传视频文件的时候,在选取好了文件或者拍摄好了视频点击确定之后,文件选择界面关闭,但是文件并没有上传.而 <input type