-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
101 lines (91 loc) · 2.36 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Created by haojing on 16/2/16.
*/
var qiniu = require('qiniu');
var fs = require('fs');
qiniu.conf.ACCESS_KEY = '<自己的>';
qiniu.conf.SECRET_KEY = '<自己的>';
//bucketname空间名称
function uptoken(bucketname) {
var putPolicy = new qiniu.rs.PutPolicy(bucketname);
return putPolicy.token();
}
/**
* 上传二进制文件
* @param bucketDomain 空间名称
* @param key 七牛上文件名称
* @param uptoken 凭证
*/
function uploadBuf(body, key, uptoken) {
var extra = new qiniu.io.PutExtra();
qiniu.io.put(uptoken, key, body, extra, function(err, ret) {
if(!err) {
console.log('上传成功');
} else {
console.log(err);
}
})
}
/**
* 上传本地文件
* @param bucketDomain 空间名称
* @param key 七牛上文件名称
* @param uptoken 凭证
*/
function upload(localFile, key, uptoken) {
var extra = new qiniu.io.PutExtra();
qiniu.io.putFile(uptoken, key, localFile, extra, function (err, ret) {
if(!err) {
console.log('上传成功');
} else {
console.log(err);
}
})
}
var client = new qiniu.rs.Client();
/**
* 删除文件
* @param bucketDomain 空间名称
* @param key 七牛上文件名称
*/
function deleteFile(bucketname, key ) {
client.remove(bucketname, key, function(err, ret) {
if (!err) {
console.log('删除成功');
} else {
console.log(err);
}
})
}
/**
* 查看图像信息
* @param bucketDomain 域名
* @param key 七牛上文件名称
* @param cb 回调函数
*/
function getImageInfo (bucketDomain, key, cb) {
var url = qiniu.rs.makeBaseUrl(bucketDomain, key);
var ii = new qiniu.fop.ImageInfo();
url == ii.makeRequest(url);
//共有的bucket省略此步骤
// var policy = new qiniu.rs.GetPolicy();
// url = policy.makeRequest(url);
cb('', url);
}
/**
* 生成缩略图
* @param bucketDomain 域名
* @param key 七牛上文件名称
* @param cb 回调函数
*/
function generatorSmallImage (bucketDomain, key, cb) {
var url = qiniu.rs.makeBaseUrl(bucketDomain, key);
var iv = new qiniu.fop.ImageView();
iv.width = 100;
url = iv.makeRequest(url);
cb('', url);
}
//upload('test.jpg', 'wojiushihis.jpg', uptoken('hjqiniu888'));
exports.uptoken = uptoken;
exports.uploadBuf = uploadBuf;
exports.upload = upload;