Skip to content

Instantly share code, notes, and snippets.

@lintianzhi
Created January 2, 2016 14:23
Show Gist options
  • Save lintianzhi/85aad3a91ea0d8a32ea0 to your computer and use it in GitHub Desktop.
Save lintianzhi/85aad3a91ea0d8a32ea0 to your computer and use it in GitHub Desktop.
//import request from 'superagent';
request = require('superagent')
querystring = require('querystring');
url = require('url');
crypto = require('crypto');
__qiniuAccessKey = 'xxxxx';
__qiniuSecretKey = 'xxxxx';
function makezip() {
uri = 'http://api.qiniu.com/pfop/';
//key = 'uploads/123456.jpg';
key = 'gogopher.jpg';
photoUrl = 'http://7xizen.com1.z0.glb.clouddn.com/uploads/123456.jpg';
photoUrl = 'url/' + encodeUrlSafeBase64(photoUrl);
saveas = 'contest:test.zip';
saveas = '|saveas/' + encodeUrlSafeBase64(saveas);
param = {
bucket: 'ztest',
key,
fops: 'mkzip/2/' + photoUrl + saveas,
force: 1
}
body = querystring.stringify(param);
pathname = url.parse(uri).path;
access = pathname + '\n' + body;
digest = hmacSha1(access, __qiniuSecretKey);
digest = digest.replace(/\+/g, '-').replace(/\//g, '_');
accessToken = 'QBox ' + __qiniuAccessKey + ':' + digest;
request
.post(uri)
.set('Content-Type', 'application/x-www-form-urlencoded')
.set('Authorization', accessToken)
.send(body)
.end(function(err, res) {
console.log(res.body);
})
}
function encodeUrlSafeBase64(string) {
s = new Buffer(string).toString('base64');
s = s.replace(/\+/g, '-').replace(/\//g, '_');
return s;
}
function hmacSha1(encodedFlags, secretKey) {
var hmac = crypto.createHmac('sha1', secretKey);
hmac.update(encodedFlags);
return hmac.digest('base64');
}
makezip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment