node.js - 如何将 Assets 从 node.js 上传到 github 版本

标签 node.js github-api

我正在尝试在我以编程方式创建的 Github 版本上自动发布一些 Assets 。

这是我的上传功能:

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 not found

我尝试通过 token 和用户/密码进行身份验证

我检查了网址

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

有什么线索吗?谢谢!

最佳答案

我通过不使用低级 node.js 模块而是使用 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 切换到至 request .

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
}));

可以通过 get request 检索 upload_uri在现有版本或 the response directly after the release creation 中.

关于node.js - 如何将 Assets 从 node.js 上传到 github 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26678198/

相关文章:

ruby - github api v3 创建问题消息未找到

javascript - octokit getLabel 返回 TypeError : callback. 绑定(bind)不是函数

jwt - 为什么 id_token 通过带有片段标识符而不是查询字符串的 url 传递?

Android PushUp 数据更改通知

javascript - JS中如何创建私有(private)变量

node.js - meteor 推送到heroku : cannot find main. js

git - GitHub 中的 Release 到底是什么?

java - 使用 Java API 从 GitHub 获取所有提交

ios - 无法使用 JSONJoy 解析来自 Socket.IO 的 JSON 数据

javascript - 在不创建重新创建服务器的情况下测试 Express 中间件的简单方法?