2013-04-29 2 views
0

nodejs (단계 3에서 https://developer.vimeo.com/apis/advanced/upload)를 사용하여 vimeo에 동영상을 업로드하려고합니다.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); 
     } 
    }); 
}; 

파일을 읽을 때 :

(오류) 16,
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

내가 콘텐츠 형식과 내용 길이를 통과해야합니다. 이 파이프 함수를 사용하여 옵트에서 어떻게 설정할 수 있습니까? (content-length를 data.length와 동일하게 설정해야 함) – Ojtwist

+0

요청 추가 정보를 보셨습니까? 옵션 개체에 원하는 헤더를 지정할 수 있습니다 (위의 예제에서 선택). – Noah

+0

네가 파일을 vimeo로 파이핑하고 있지만 알고 있습니다. 나는 content-length를 그 파일의 크기로 설정해야하고, 보내야 할 파일을 포함하고있는 데이터 객체가 없기 때문에 파이핑 할 때 이것을 어떻게하는지 모른다. – Ojtwist

관련 문제