2013-04-29 120 views
0

我試圖用nodejs將視頻上傳到vimeo(步驟3中爲https://developer.vimeo.com/apis/advanced/upload)。這是我目前做的:使用nodejs將視頻上傳到Vimeo

首先,我調用該函數來讀取文件:

var options = { 
     hostname : dataObject.ticket.host, 
     path : '/upload?ticket_id=' + dataObject.ticket.id, 
     port : 8080, 
     method: 'POST' 
     } 

postMovie(options); 

我從我的對象,這些參數:

{ 
    "generated_in": "0.0308", 
    "stat": "ok", 
    "ticket": { 
     "endpoint": "http://126535.cloud.vimeo.com:8080/upload?ticket_id=9d818e8bd066dfd54e53f1be2fa3f958", 
     "endpoint_secure": "https://126535.cloud.vimeo.com/upload?ticket_id=9d818e8bd066dfd54e53f1be2fa3f958", 
     "host": "126535.cloud.vimeo.com", 
     "id": "9d818e8bd066dfd54e53f1be2fa3f958", 
     "max_file_size": "26843545600" 
    } 
} 

該功能被稱爲:

function postMovie(options){ 
    // This is an async file read 
    fs.readFile('public/uploads/4363066343.mp4', function (err, data) { 
     if (err) { 

     console.log("FATAL An error occurred trying to read in the file: " + err); 
     process.exit(-2); 
     } 
     // Make sure there's data before we post it 
     if(data) { 
     PostData(data,options); 
     } 
     else { 
     console.log("No data to post"); 
     process.exit(-1); 
     } 
    }); 
}; 

當文件被讀取時:

function PostData(data,options) { 

     var headers = { 
      'Content-Type': 'video/mp4', 
      'Content-Length': data.length 
     } 

     options.headers = headers 

     console.log(options) 

    // Set up the request 
    var post_req = http.request(options, function(res) { 
     res.on('data', function (chunk) { 
      console.log('Response: ' + chunk); 
     }); 
    }); 

    // post the data 
    post_req.write(data); 
    post_req.end(); 

    post_req.on('error', function(e) { 
     console.log('problem with request: ' + e.message); 
    }); 
} 

我post_req.on(錯誤)記錄本:

problem with request: write EPIPE 
problem with request: write EPIPE 

我理解,這是因爲在服務器端超時的。

我假設我的要求沒有形成。

有人能指出我做錯了什麼嗎?

回答

0

request模塊的上傳操作將會簡單得多。

var inspect = require('eyespect').inspector(); 
var request = require('request') 
var path = require('path') 
var fs = require('fs') 
var filePath = path.join(__dirname, '../public/uploads/foo.mp4') 
fs.stat(filePath, function(err, stats) { 
    if (err) { 
    inspect(err, 'error stating file') 
    return 
    } 
    var fileSize = stats.size 
    var url = 'https://126535.cloud.vimeo.com/upload?ticket_id=9d818e8bd066dfd54e53f1be2fa3f958' 
    var opts = { 
    url: url, 
    method: 'post', 
    headers: { 
     'Content-Length': fileSize, 
     'Content-Type': 'foo' 
    } 
    var r = request(opts) 

    // pipe the file on disk to vimeo 
    var readStream = fs.createReadStream(filePath) 
    readStream.pipe(r) 
    readStream.on('error', function (err) { 
    inspect(err, 'error uploading file') 
    }) 
    readStream.on('end', function (err) { 
    inspect('file uploaded correctly') 
    }) 
}) 

請求,您還可以,如果該文件是大集timeout選項以及因而需要較長的時間來上傳

+0

我必須通過內容類型和內容長度。如何使用此管道功能在opts中設置這些值? (需要設置內容長度等於data.length) – Ojtwist 2013-04-29 14:29:42

+0

您是否看過請求自述文件?您可以在選項對象中指定任何標題(在上面的示例中選擇opts) – Noah 2013-04-29 14:38:45

+0

是的我知道,儘管您將文件傳輸到vimeo。我需要將內容長度設置爲該文件的大小,並且我不知道如何在管道時執行此操作,因爲我沒有包含必須發送的文件的數據對象。 – Ojtwist 2013-04-29 14:42:34