繁体   English   中英

如何将资产从node.js上传到github版本

[英]How to upload assets to a github release from node.js

我试图在编程创建的Github版本上自动发布一些资产。

这是我的上传功能:

function uploadFile(fileName, uploadUrl, callback){

  var uploadEndPoint = url.parse(uploadUrl.substring(0,uploadUrl.indexOf('{')));

  options.host = uploadEndPoint.hostname;
  options.path = uploadEndPoint.pathname+'?name=' + fileName;
  options.method = 'POST';
  options.headers['content-type'] = 'application/zip';

  var uploadRequest = https.request(options, callback);

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

  var readStream = fs.ReadStream(path.resolve(__dirname,'builds',fileName));
  readStream.pipe(uploadRequest);

  readStream.on('close', function () {
    req.end();
    console.log('release.js - ' + fileName + ' sent to the server');
  });
}

在这结束时我得到了404找不到

我从令牌和用户/密码尝试了auth

我检查了网址

我虽然可能是因为SNI,但我不知道如何检查。

任何线索? 谢谢 !

我通过不使用低级node.js模块并使用restler来找到解决该问题的方法,而restler是一个处理SNI的库。

以下是如何使用它:

  rest = require('restler'),
  path = require('path'),
  fs = require('fs');

  fs.stat(path.resolve(__dirname,'builds',fileName), function(err, stats){
    rest.post(uploadEndPoint.href+'?name=' + fileName, {
      multipart: true,
      username: GITHUB_OAUTH_TOKEN,
      password: '',
      data: rest.file(path.resolve(__dirname,'builds',fileName), null, stats.size, null, 'application/zip')
    }).on('complete', callback);
  });

希望能帮助别人:)

编辑于2015年2月27日:我们最近从restler切换到请求

var 
 request  = require('request'),
 fs = require('fs');

var stats = fs.statSync(filePath);

var options = {
  url: upload_url.replace('{?name}', ''),
  port: 443,
  auth: {
    pass: 'x-oauth-basic',
    user: GITHUB_OAUTH_TOKEN
  },
  json:true,
  headers: {
    'User-Agent': 'Release-Agent',
    'Accept': 'application/vnd.github.v3+json',
    'Content-Type': 'application/zip',
    'Content-Length': stats.size
  },
  qs: {
    name: assetName
  }
};

// Better as a stream
fs.createReadStream(filePath).pipe(request.post(options, function(err, res){
  // Do whatever you will like with the result
}));

upload_uri可以通过现有版本上的get请求或在创建发布后直接在响应中检索。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM