Skip to content

Commit

Permalink
修改字段和用例
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Oct 30, 2020
1 parent b8922d4 commit fa785ae
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 28 deletions.
20 changes: 4 additions & 16 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,7 @@ var selectCsvOpt = {
QuoteEscapeCharacter: "\""
}
},
RequestProgress: {
Enabled: "FALSE"
}
RequestProgress: {Enabled: "FALSE"}
},
};

Expand All @@ -906,19 +904,9 @@ var selectJsonOpt = {
SelectRequest: {
Expression: "Select * from COSObject",
ExpressionType: "SQL",
InputSerialization: {
JSON: {
Type: "DOCUMENT",
},
},
OutputSerialization: {
JSON: {
RecordDelimiter: "\n"
},
},
RequestProgress: {
Enabled: "FALSE"
}
InputSerialization: {JSON: {Type: "DOCUMENT"}},
OutputSerialization: {JSON: {RecordDelimiter: "\n"}},
RequestProgress: {Enabled: "FALSE"}
},
};

Expand Down
4 changes: 2 additions & 2 deletions sdk/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2595,8 +2595,8 @@ function selectObjectContent(params, callback) {
};
// 只要流里有解析出 stats,就返回 Stats
if (selectResult.stats) result.Stats = selectResult.stats;
// 只要有 records,就返回 PayLoad
if (selectResult.records) result.PayLoad = Buffer.concat(selectResult.records);
// 只要有 records,就返回 Payload
if (selectResult.records) result.Payload = Buffer.concat(selectResult.records);
callback(null, result);
});
if (!params.ReturnStream && params.DataType !== 'raw') {
Expand Down
159 changes: 149 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var COS = require('../index');
var request = require('request');
var util = require('../demo/util');
var config = require('../demo/config');
var Writable = require('stream').Writable;
var Stream = require('stream');

var Writable = Stream.Writable;

var dataURItoUploadBody = function (dataURI) {
return Buffer.from(dataURI.split(',')[1], 'base64');
Expand Down Expand Up @@ -539,6 +541,7 @@ group('putObject()', function () {
var outputStream = new Writable({
write: function (chunk, encoding, callback) {
objectContent = Buffer.concat([objectContent, chunk]);
callback();
}
});
setTimeout(function () {
Expand Down Expand Up @@ -750,7 +753,7 @@ group('putObject()', function () {
});
});

group('getObject()', function () {
group('getObject(),getObjectStream()', function () {
test('getObject() body', function (done, assert) {
var key = '1.txt';
var content = Date.now().toString();
Expand Down Expand Up @@ -780,6 +783,7 @@ group('getObject()', function () {
var outputStream = new Writable({
write: function (chunk, encoding, callback) {
objectContent = Buffer.concat([objectContent, chunk]);
callback();
}
});
var content = Date.now().toString(36);
Expand Down Expand Up @@ -810,6 +814,59 @@ group('getObject()', function () {
});
});
});
test('getObjectStream', function (done, assert) {
var content = Date.now().toString();
var key = '1.json';
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
Body: content,
}, function (err, data) {
var bufList = [];
var writeStream = new Writable({
write: function (chunk, encoding, callback) {
bufList.push(chunk);
callback();
},
});
cos.getObjectStream({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
}, function (err, data) {
assert.ok(Buffer.concat(bufList).toString() === content);
done();
}).pipe(writeStream);
});
});
test('getObject Output', function (done, assert) {
var content = Date.now().toString();
var key = '1.json';
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
Body: content,
}, function (err, data) {
var bufList = [];
var writeStream = new Writable({
write: function (chunk, encoding, callback) {
bufList.push(chunk);
callback();
},
});
cos.getObject({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
Output: writeStream,
}, function (err, data) {
assert.ok(Buffer.concat(bufList).toString() === content);
done();
});
});
});
});

group('Key 特殊字符', function () {
Expand Down Expand Up @@ -2198,14 +2255,15 @@ group('upload Content-Type', function () {
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: '1.txt',
Key: '1',
Body: '',
}, function (err, data) {
cos.headObject({
Bucket: config.Bucket,
Region: config.Region,
Key: '1',
}, function (err, data) {
console.log(data.headers['content-type']);
assert.ok(data.headers['content-type'] === 'application/octet-stream', 'Content-Type 正确');
done();
});
Expand Down Expand Up @@ -2583,13 +2641,6 @@ group('BucketInventory', function () {
Region: config.Region,
Id: InventoryConfiguration.Id
}, function(err, data) {
console.log('-----------------------------');
console.log(JSON.stringify(config));
console.log('-----------------------------');
console.log(JSON.stringify(InventoryConfiguration));
console.log('-----------------------------');
console.log(JSON.stringify(data.InventoryConfiguration));
console.log('-----------------------------');
assert.ok(comparePlainObject(InventoryConfiguration, data.InventoryConfiguration));
done();
});
Expand Down Expand Up @@ -3016,3 +3067,91 @@ group('Query 的键值带有特殊字符', function () {
});
});
});

group('selectObjectContent(),selectObjectContentStream()', function () {
var key = '1.json';
var selectJsonOpt = {
Bucket: config.Bucket,
Region: config.Region,
Key: key,
SelectType: 2,
SelectRequest: {
Expression: "Select * from COSObject",
ExpressionType: "SQL",
InputSerialization: {JSON: {Type: "DOCUMENT",},},
OutputSerialization: {JSON: {RecordDelimiter: "\n"},},
RequestProgress: {Enabled: "FALSE"}
},
};
test('selectObjectContent', function (done, assert) {
var time = Date.now();
var content = `{"a":123,"b":"${time}","c":{"d":456}}`;
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
Body: content,
}, function (err, data) {
var bufList = [];
var writeStream = new Writable({
write: function (chunk, encoding, callback) {
bufList.push(chunk);
callback();
},
});
cos.selectObjectContent(selectJsonOpt, function (err, data) {
assert.ok(data.Payload.toString() === content + '\n');
done();
});
});
});
test('selectObjectContentStream', function (done, assert) {
var time = Date.now();
var content = `{"a":123,"b":"${time}","c":{"d":456}}`;
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
Body: content,
}, function (err, data) {
var bufList = [];
var writeStream = new Writable({
write: function (chunk, encoding, callback) {
bufList.push(chunk);
callback();
},
});
cos.selectObjectContentStream(selectJsonOpt, function (err, data) {
assert.ok(Buffer.concat(bufList).toString() === content + '\n');
done();
}).pipe(writeStream);
});
});
test('selectObjectContentStream raw', function (done, assert) {
var time = Date.now();
var content = `{"a":123,"b":"${time}","c":{"d":456}}`;
var key = '1.json';
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
Body: content,
}, function (err, data) {
var bufList = [];
var writeStream = new Writable({
write: function (chunk, encoding, callback) {
bufList.push(chunk);
callback();
},
});
cos.selectObjectContentStream({
...selectJsonOpt,
DataType: 'raw',
}, function (err, data) {
var result = Buffer.concat(bufList).toString();
assert.ok(result.includes('<BytesScanned>') && result.includes(content));
done();
}).pipe(writeStream);
});
});
});

0 comments on commit fa785ae

Please sign in to comment.