UNPKG

ks3

Version:

本代码库为`金山云存储KS3`服务.主要提供`KS3 nodejs SDK`和`命令行工具`.

463 lines (424 loc) 10.8 kB
var KS3 = require('../..') var should = require('should'); require('should-http'); var path = require('path'); var fs = require('fs'); const { ak, sk } = require('../config') const bucketName = 'test-sdk-zwy-qingdao' let Endpoint = 'ks3-cn-qingdao.ksyuncs.com' describe('API Object', function() { var client = new KS3(ak, sk, bucketName); client.config({ dataType: 'json' }) var key = 'word.txt' var acl = 'public-read' var storageClass = 'ARCHIVE' var ServerSideEncryption = 'AES256' before (function (done) { this.timeout(30000) client.bucket.put({Endpoint}, function(err, data, res) { if (!err) { client.object.put({ Key: key, FilePath: path.join(__dirname, '../assets/' + key), ACL: acl, Endpoint }, function (err, data, res) { should.not.exist(err) done() }) } }); }) describe('put ', function() { describe('upload a file', function() { it('put object and set acl', function(done) { var content = 'Hello world'; var key = 'test_upload_acl.txt'; var acl = 'public-read-write'; client.object.put({ Bucket: bucketName, Key: key, Body: content, ACL: acl, Endpoint }, function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }); }); }); }); describe('get ', function () { it('Range header', function (done) { client.object.get({ Key: key, headers: { Range: 'bytes=0-9' }, Endpoint }, function (err, data, res) { should.equal(res.caseless.get('content-length'), '10') // should.equal(Buffer.isBuffer(data), true) should.equal(data.toString(), '申明:本') done() }) }) }) describe('upload a big size file by multitpart ', function() { it('completes a multipart upload by assembling previously uploaded parts', function(done) { client.config({ dataType: 'json' }); var key = 'bigFile.mov'; client.object.multitpart_upload_init({ Key: key, Endpoint }, function(err, data, res) { if (err) throw err; var uploadId = data.InitiateMultipartUploadResult.UploadId; client.config({ dataType: 'xml' }); client.object.upload_part({ Key: key, PartNumber: 1, Body: 'API Object put test : uploads a part in a multipart upload', UploadId: uploadId, Endpoint }, function(err, data, res) { if (err) throw err; var etag = res.headers.etag; client.object.upload_complete({ Key: key, UploadId: uploadId, Body: (function() { var sample = ['<CompleteMultipartUpload>', '<Part>', '<PartNumber>' + 1 + '</PartNumber>', '<ETag>' + etag + '</ETag>', '</Part>', '</CompleteMultipartUpload>']; return sample.join(''); })(), Endpoint }, function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }) }); }); }); it('aborts a multipart upload', function(done) { client.config({ dataType: 'json' }); var key = 'bigFile1.mov'; client.object.multitpart_upload_init({ Key: key, Endpoint }, function(err, data, res) { if (err) throw err; var uploadId = data.InitiateMultipartUploadResult.UploadId client.object.upload_abort({ Key: key, UploadId: uploadId, Endpoint }, function(err, data, res) { should.not.exist(err); res.should.have.status(204); done(); }); }); }); it('lists the parts that have been uploaded for a specific multipart upload', function(done) { client.config({ dataType: 'json' }); var key = 'bigFile.mov'; client.object.multitpart_upload_init({ Key: key, Endpoint }, function(err, data, res) { var uploadId = data.InitiateMultipartUploadResult.UploadId client.config({ dataType: 'xml' }); client.object.upload_part({ Key: key, PartNumber: 1, Body: 'API Object put test : uploads a part in a multipart upload', UploadId: uploadId, Endpoint }, function(err, data, res) { if (err) throw err; client.object.upload_list_part({ Key: key, UploadId: uploadId, Endpoint }, function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }) }); }); }); }); describe('all of object process', function() { var content = 'Hello world'; var key = 'test_upload.txt'; before(function() { client.object.put({ Bucket: bucketName, Key: key, Body: content, Endpoint }, function(){ }) }); it('should be return string with get object', function(done) { client.config({ dataType: 'json' }); client.object.get({ Bucket: bucketName, Key: key, Endpoint }, function(err, data, res) { should.not.exist(err); res.should.have.status(200); (data.toString() === content).should.be.ok; done(); }) }); // HEAD object it('should be return a 200 statuscode with head object', function(done) { client.object.head({ Key: key, Endpoint }, function(err, data, res) { should.not.exist(err); res.should.have.status(200); done() }) }); it('should be return a 200 statuscode with put object ACL', function(done) { // putAcl 测试 client.object.putAcl({ Key: key, ACL: 'public-read', Endpoint }, function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }) }); it('should be return a json object with get object ACL', function(done) { client.config({ dataType: 'json' }); // getAcl测试 client.object.getAcl({ Key: key, Endpoint }, function(err, data, res) { var d = data.AccessControlPolicy.AccessControlList.Grant; (d[0]['Permission'] === 'FULL_CONTROL' && d[1]['Permission'] === 'READ').should.be.ok; should.not.exist(err); res.should.have.status(200); done(); }) }); it('copy object', function (done) { client.object.copy({ destinationBucket: bucketName, destinationObject: key + '-copy', sourceBucket: bucketName, sourceKey: key, Endpoint }, function (err, data, res) { should.not.exist(err) done() }) }) // generatePresignedUrl it('generatePresignedUrl ', function (done) { client.object.generatePresignedUrl({ Key: key, expiration: 2000 }, function(err, url, res) { should.not.exist(err) url.should.startWith('http'); done() }) }) // putObjectTagging it('put object tagging', function (done) { client.object.putObjectTagging({ Key: key, Taggings: [{ key: '22', value: 'aa' }], Endpoint }, function(err, data, res) { should.not.exist(err) done() }) }) // getObjectTagging it('get object tagging', function (done) { client.object.getObjectTagging({ Key: key, Endpoint }, function(err, data, res) { should.not.exist(err) done() }) }) // deleteObjectTagging it('delete object tagging', function (done) { client.object.deleteObjectTagging({ Key: key, Endpoint }, function(err, data, res) { should.not.exist(err) done() }) }) var newName = key + '-newName' // rename it('rename object', function (done) { client.object.rename({ Key: key, newKey: newName, Endpoint }, function(err, data, res) { should.not.exist(err) done() }) }) // modifyStorageClass it('modify storageClass object', function (done) { client.object.modifyStorageClass({ Key: newName, storageClass, Endpoint }, function(err, data, res) { should.not.exist(err) done() }) }) // restore it('restore object', function (done) { client.object.restore({ Key: newName, Endpoint }, function(err, data, res) { should.not.exist(err) done() }) }) it('should be return 204 statuscode with delete object', function(done) { // 都跑过之后 删除object client.object.del({ Bucket: bucketName, Key: newName, Endpoint }, function(err, data, res) { should.not.exist(err); res.should.have.status(204); // 删除成功返回的是204 done(); }) }); }); // 加密相关 describe('encrypt', function () { it('return a error, only AES256 supported.', function() { (function() { client.object.put({ Body: 'This is a encrypt content ~', Key: key, ServerSideEncryption: 'AES128', Endpoint }, function (){}) }).should.throw ('check your encrypt type, we now only support "AES256".'); }) it('upload and set encrypt', function(done) { client.object.put({ Body: 'This is a encrypt content ~', Key: key, ServerSideEncryption, Endpoint }, function (err, data, res){ should.not.exist(err) res.should.have.status(200) done() }) }) it('copy encrypt file', function (done) { client.object.copy({ destinationBucket: bucketName, destinationObject: key + '-copy', sourceBucket: bucketName, sourceKey: key, Endpoint }, function (err, data, res) { should.not.exist(err) done() }) }) it('update file storageClass', function (done) { client.object.modifyStorageClass({ Key: key, storageClass: 'STANDARD_IA', Endpoint }, function (err, data, res) { should.not.exist(err) done() }) }) it('rename encrypt file', function(done) { client.object.rename({ Key: key, newKey: 'new-' + key, Endpoint }, function (err, data, res) { should.not.exist(err) done() }) }) }) after(function (done) { client.config({ dataType: 'json' }); this.timeout(30000) // 获取bucket数据 --> 循环删除object --> 删除bucket client.bucket.get({Endpoint}, function(err, data, res){ if (err) { throw Error(err) } const objects = data.ListBucketResult.Contents const promiseQueue = [] objects.forEach(item => { const p = new Promise(function(resolve, reject){ client.object.del({ Key: item.Key, Endpoint }, function (err, data, res) { if (err) { reject(err) } resolve() }) }) promiseQueue.push(p) }) Promise.all(promiseQueue).then(val => { client.bucket.del({Endpoint}, function (err, data, res) { should.not.exist(err) res.should.have.status(204) done() }) }).catch(err => { done() }) }) }); });