Skip to content

Commit 8cff2af

Browse files
carsonxucarsonxu
carsonxu
authored and
carsonxu
committed
改成对象调用
1 parent d6b89ba commit 8cff2af

File tree

14 files changed

+3334
-3039
lines changed

14 files changed

+3334
-3039
lines changed

demo/PutObject.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

demo/SliceUploadFile.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

demo/config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var config = {
2+
AppId: '1250000000',
3+
SecretId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
4+
SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
5+
};
6+
7+
module.exports = config;

demo/demo.js

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var COS = require('../index');
4+
var util = require('./util');
5+
var config = require('./config');
6+
7+
var cos = new COS(config);
8+
9+
function getService() {
10+
cos.getService({}, function (err, data) {
11+
return console.log(err || data);
12+
});
13+
}
14+
15+
function putObject() {
16+
// 创建测试文件
17+
var filename = '1mb.zip';
18+
util.createFile(path.resolve(__dirname, filename), 1024 * 1024, function (err) {
19+
// 调用方法
20+
var filepath = path.resolve(__dirname, filename);
21+
cos.putObject({
22+
Bucket: 'test', /* 必须 */
23+
Region: 'cn-south',
24+
Key: filename, /* 必须 */
25+
Body: fs.createReadStream(filepath), /* 必须 */
26+
ContentLength: fs.statSync(filepath).size, /* 必须 */
27+
}, function (err, data) {
28+
if (err) {
29+
console.log(err);
30+
} else {
31+
console.log(JSON.stringify(data, null, ' '));
32+
}
33+
});
34+
});
35+
}
36+
37+
function deleteObject() {
38+
cos.deleteObject({
39+
Bucket: 'test',
40+
Region: 'cn-south',
41+
Key: '1mb.zip'
42+
}, function (err, data) {
43+
if (err) {
44+
return console.log(err);
45+
}
46+
47+
console.log(JSON.stringify(data, null, ' '));
48+
});
49+
}
50+
51+
function getBucket() {
52+
cos.getBucket({
53+
Bucket: 'test',
54+
Region: 'cn-south'
55+
}, function (err, data) {
56+
if (err) {
57+
return console.log(err);
58+
}
59+
60+
console.log(JSON.stringify(data, null, ' '));
61+
});
62+
}
63+
64+
function headBucket() {
65+
cos.headBucket({
66+
Bucket: 'test',
67+
Region: 'cn-south'
68+
}, function (err, data) {
69+
if (err) {
70+
return console.log(err);
71+
}
72+
console.log(JSON.stringify(data, null, ' '));
73+
});
74+
}
75+
76+
function putBucket() {
77+
cos.putBucket({
78+
Bucket: 'test-new',
79+
Region: 'cn-south'
80+
}, function (err, data) {
81+
if (err) {
82+
return console.log(err);
83+
}
84+
console.log(JSON.stringify(data, null, ' '));
85+
});
86+
}
87+
88+
function deleteBucket() {
89+
cos.deleteBucket({
90+
Bucket: 'test-new',
91+
Region: 'cn-south'
92+
}, function (err, data) {
93+
if (err) {
94+
return console.log(err);
95+
}
96+
console.log(JSON.stringify(data, null, ' '));
97+
});
98+
}
99+
100+
function getBucketACL() {
101+
cos.getBucketACL({
102+
Bucket: 'test',
103+
Region: 'cn-south'
104+
}, function (err, data) {
105+
if (err) {
106+
return console.log(err);
107+
}
108+
109+
console.log(data.AccessControlList.Grant);
110+
});
111+
}
112+
113+
function putBucketACL() {
114+
// 该接口存在问题,不可以设置 ACL 为 'public-read' 也不能设置 GrandWrite 等
115+
cos.putBucketACL({
116+
Bucket: 'test',
117+
Region: 'cn-south',
118+
//GrantWrite : 'uin="1111", uin="2222"',
119+
ACL: 'public-read',
120+
// ACL: 'private'
121+
}, function (err, data) {
122+
if (err) {
123+
return console.log(err);
124+
}
125+
126+
console.log(JSON.stringify(data, null, ' '));
127+
});
128+
}
129+
130+
function getBucketCORS() {
131+
cos.getBucketCORS({
132+
Bucket: 'test',
133+
Region: 'cn-south'
134+
}, function (err, data) {
135+
if (err) {
136+
return console.log(err);
137+
}
138+
console.log(JSON.stringify(data, null, ' '));
139+
});
140+
}
141+
142+
function putBucketCORS() {
143+
// 该接口存在问题,Content-MD5 错误
144+
cos.putBucketCORS({
145+
Bucket: 'test',
146+
Region: 'cn-south',
147+
CORSRules: [{
148+
"AllowedOrigin": ["*"],
149+
"AllowedMethod": ["PUT", "GET", "POST", "DELETE", "HEAD"],
150+
"AllowedHeader": ["origin", "accept", "content-type", "authorzation"],
151+
"ExposeHeader": ["ETag"],
152+
"MaxAgeSeconds": "300"
153+
}]
154+
}, function (err, data) {
155+
if (err) {
156+
return console.log(err);
157+
}
158+
159+
console.log(JSON.stringify(data, null, ' '));
160+
});
161+
}
162+
163+
function getBucketLocation() {
164+
cos.getBucketLocation({
165+
Bucket: 'test',
166+
Region: 'cn-south'
167+
}, function (err, data) {
168+
if (err) {
169+
return console.log(err);
170+
}
171+
});
172+
}
173+
174+
function getObject() {
175+
cos.getObject({
176+
Bucket: 'test',
177+
Region: 'cn-south',
178+
Key: '1mb.zip',
179+
Output: fs.createWriteStream(path.resolve(__dirname, '1mb.out.zip'))
180+
}, function (err, data) {
181+
if (err) {
182+
return console.log(err);
183+
}
184+
console.log(JSON.stringify(data, null, ' '));
185+
});
186+
}
187+
188+
function headObject() {
189+
cos.headObject({
190+
Bucket: 'test',
191+
Region: 'cn-south',
192+
Key: '1mb.zip'
193+
}, function (err, data) {
194+
if (err) {
195+
return console.log(err);
196+
}
197+
console.log(JSON.stringify(data, null, ' '));
198+
});
199+
}
200+
201+
var util = require('util');
202+
var inspect = require('eyes').inspector({maxLength: false})
203+
function getObjectACL() {
204+
cos.getObjectACL({
205+
Bucket: 'test',
206+
Region: 'cn-south',
207+
Key: '1mb.zip'
208+
}, function (err, data) {
209+
if (err) {
210+
return console.log(err);
211+
}
212+
console.log(JSON.stringify(data, null, ' '));
213+
});
214+
}
215+
216+
function sliceUploadFile() {
217+
// 创建测试文件
218+
var filename = '3mb.zip';
219+
util.createFile(path.resolve(__dirname, filename), 1024 * 1024 * 3, function (err) {
220+
// 调用方法
221+
cos.sliceUploadFile({
222+
Bucket: 'test', /* 必须 */
223+
Region: 'cn-south',
224+
Key: 'p.exe', /* 必须 */
225+
FilePath: filepath, /* 必须 */
226+
SliceSize: 1024 * 1024, //1MB /* 非必须 */
227+
AsyncLimit: 5, /* 非必须 */
228+
onProgress: function (progressData, percent) {
229+
console.log(progressData, percent);
230+
},
231+
onHashProgress: function (hashProgress, percent) {
232+
console.log(hashProgress, percent);
233+
}
234+
}, function (err, data) {
235+
if (err) {
236+
console.log(err);
237+
} else {
238+
console.log(JSON.stringify(data, null, ' '));
239+
}
240+
});
241+
});
242+
}
243+
244+
function putBucketPolicy() {
245+
cos.putBucketPolicy({
246+
Policy: {
247+
"version": "2.0",
248+
"principal": {"qcs": ["qcs::cam::uin/909600000:uin/909600000"]}, // 这里的 909600000 是 QQ 号
249+
"statement": [
250+
{
251+
"effect": "allow",
252+
"action": [
253+
"name/cos:GetBucket",
254+
"name/cos:PutObject",
255+
"name/cos:PostObject",
256+
"name/cos:PutObjectCopy",
257+
"name/cos:InitiateMultipartUpload",
258+
"name/cos:UploadPart",
259+
"name/cos:UploadPartCopy",
260+
"name/cos:CompleteMultipartUpload",
261+
"name/cos:AbortMultipartUpload",
262+
"name/cos:AppendObject"
263+
],
264+
"resource": ["qcs::cos:cn-south:uid/1250000000:test-1250000000.cn-south.myqcloud.com//1250000000/test/*"] // 1250000000 是 appid
265+
}
266+
]
267+
},
268+
Bucket: 'test',
269+
Region: 'cn-south'
270+
}, function (err, data) {
271+
if (err) {
272+
console.log(err);
273+
} else {
274+
getBucketPolicy();
275+
}
276+
});
277+
}
278+
279+
function getBucketPolicy() {
280+
cos.getBucketPolicy({
281+
Bucket: 'test',
282+
Region: 'cn-south'
283+
}, function (err, data) {
284+
if (err) {
285+
console.log(err);
286+
} else {
287+
console.log(JSON.stringify(data, null, ' '));
288+
}
289+
});
290+
}
291+
292+
function putObjectCopy() {
293+
cos.putObjectCopy({
294+
Bucket: 'test',
295+
Region: 'cn-south',
296+
Key: '1mb.copy.zip',
297+
CopySource: 'test-1251902136.cn-south.myqcloud.com/1mb.zip',
298+
}, function (err, data) {
299+
if (err) {
300+
console.log(err);
301+
} else {
302+
console.log(JSON.stringify(data, null, ' '));
303+
}
304+
});
305+
}
306+
307+
getService();
308+
// getBucket();
309+
// headBucket();
310+
// putBucket();
311+
// deleteBucket();
312+
// getBucketACL();
313+
// putBucketACL();
314+
// getBucketCORS();
315+
// putBucketCORS();
316+
// putBucketPolicy();
317+
// getBucketPolicy();
318+
// getBucketLocation();
319+
// getObject();
320+
// putObject();
321+
// putObjectCopy();
322+
// headObject();
323+
// deleteObject();
324+
// getObjectACL();
325+
// sliceUploadFile();

0 commit comments

Comments
 (0)