Skip to content

Commit 826fb25

Browse files
committed
add http & https proxy mentioned in issue 192
1 parent 7aadf97 commit 826fb25

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

examples/http_https_proxy.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const qiniu = require("../index.js");
2+
const proc = require("process");
3+
const tunnel = require('tunnel-agent');
4+
5+
var bucket = 'if-pbl';
6+
var accessKey = proc.env.QINIU_ACCESS_KEY;
7+
var secretKey = proc.env.QINIU_SECRET_KEY;
8+
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
9+
var options = {
10+
scope: bucket,
11+
}
12+
var putPolicy = new qiniu.rs.PutPolicy(options);
13+
14+
var uploadToken = putPolicy.uploadToken(mac);
15+
var config = new qiniu.conf.Config();
16+
//config.zone = qiniu.zone.Zone_z0;
17+
config.useHttpsDomain = true;
18+
var formUploader = new qiniu.form_up.FormUploader(config);
19+
var putExtra = new qiniu.form_up.PutExtra();
20+
21+
//设置HTTP(s)代理服务器,这里可以参考:https://github.com/request/tunnel-agent
22+
//有几个方法:
23+
//exports.httpOverHttp = httpOverHttp
24+
//exports.httpsOverHttp = httpsOverHttp
25+
//exports.httpOverHttps = httpOverHttps
26+
//exports.httpsOverHttps = httpsOverHttps
27+
28+
var proxyAgent = tunnel.httpOverHttp({
29+
proxy: {
30+
host: 'localhost',
31+
port: 8888
32+
}
33+
});
34+
35+
qiniu.conf.RPC_HTTP_AGENT = proxyAgent;
36+
37+
//qiniu.conf.RPC_HTTPS_AGENT = proxyAgent;
38+
//以代理方式上传
39+
formUploader.put(uploadToken, null, "hello", null, function(respErr,
40+
respBody, respInfo) {
41+
if (respErr) {
42+
throw respErr;
43+
}
44+
45+
if (respInfo.statusCode == 200) {
46+
console.log(respBody);
47+
} else {
48+
console.log(respInfo.statusCode);
49+
console.log(respBody);
50+
}
51+
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "qiniu",
3-
"version": "7.0.2",
3+
"version": "7.0.4",
44
"description": "Node wrapper for Qiniu Resource (Cloud) Storage API",
55
"main": "index.js",
66
"directories": {
@@ -55,6 +55,7 @@
5555
"crc32": "0.2.2",
5656
"urllib": "2.22.0",
5757
"agentkeepalive": "3.3.0",
58+
"tunnel-agent": "0.6.0",
5859
"urlencode": "1.1.0"
5960
},
6061
"devDependencies": {

qiniu/conf.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ exports.FormMimeRaw = "application/octet-stream";
2121
exports.RS_HOST = "http://rs.qiniu.com";
2222
exports.RPC_TIMEOUT = 60000; //60s
2323

24+
//proxy
25+
exports.RPC_HTTP_AGENT = null;
26+
exports.RPC_HTTPS_AGENT = null;
27+
2428
exports.Config = function Config(options) {
2529
options = options || {};
2630
//use http or https protocol

qiniu/rpc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ function post(requestURI, requestForm, headers, callbackFunc) {
4646
// timing: true,
4747
};
4848

49+
if (conf.RPC_HTTP_AGENT) {
50+
data['agent'] = conf.RPC_HTTP_AGENT;
51+
}
52+
53+
if (conf.RPC_HTTPS_AGENT) {
54+
data['httpsAgent'] = conf.RPC_HTTPS_AGENT;
55+
}
56+
4957
if (Buffer.isBuffer(requestForm) || typeof requestForm === 'string') {
5058
data.content = requestForm;
5159
} else if (requestForm) {

0 commit comments

Comments
 (0)