From fa785ae93e5d2d4e5786836811f19596c32798ae Mon Sep 17 00:00:00 2001 From: carsonxu <459452372@qq.com> Date: Fri, 30 Oct 2020 13:56:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AD=97=E6=AE=B5=E5=92=8C?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/demo.js | 20 ++----- sdk/base.js | 4 +- test/test.js | 159 +++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 155 insertions(+), 28 deletions(-) diff --git a/demo/demo.js b/demo/demo.js index cb4cd8c..c3780d4 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -892,9 +892,7 @@ var selectCsvOpt = { QuoteEscapeCharacter: "\"" } }, - RequestProgress: { - Enabled: "FALSE" - } + RequestProgress: {Enabled: "FALSE"} }, }; @@ -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"} }, }; diff --git a/sdk/base.js b/sdk/base.js index 3b282a2..1b2c6b3 100644 --- a/sdk/base.js +++ b/sdk/base.js @@ -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') { diff --git a/test/test.js b/test/test.js index bf6e654..f100df0 100644 --- a/test/test.js +++ b/test/test.js @@ -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'); @@ -539,6 +541,7 @@ group('putObject()', function () { var outputStream = new Writable({ write: function (chunk, encoding, callback) { objectContent = Buffer.concat([objectContent, chunk]); + callback(); } }); setTimeout(function () { @@ -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(); @@ -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); @@ -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 () { @@ -2198,7 +2255,7 @@ group('upload Content-Type', function () { cos.putObject({ Bucket: config.Bucket, Region: config.Region, - Key: '1.txt', + Key: '1', Body: '', }, function (err, data) { cos.headObject({ @@ -2206,6 +2263,7 @@ group('upload Content-Type', function () { 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(); }); @@ -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(); }); @@ -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('') && result.includes(content)); + done(); + }).pipe(writeStream); + }); + }); +});