UNPKG

ks3

Version:

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

162 lines (151 loc) 4.99 kB
var KS3 = require('..'); var should = require('should'); require('should-http'); var fs = require('fs'); var path = require('path'); var { ak, sk, bucketName, region, prefix} = require('./config') const { testTagging } = require('./util') var bucketName = 'test-sdk-' + (+new Date()); var client = new KS3(ak, sk, bucketName, region); var debug = require('debug')('test-upload'); var key = 'multipartUpload/bigFile.tmp-' + (new Date()); const fileSizeInBytes = 20 * 1024 * 1024 * 1024; var acl = 'public-read'; var headers = { 'x-kss-acl': acl, 'x-kss-tagging': 'aaa=sss&test=+ - . = _ : / @', 'x-kss-server-side-encryption': 'AES256' } var tmpFilePath = path.join(__dirname, ('./assets/bigFile200G.tmp' + (Date.now()))); /** * 测试分块拷贝 */ describe('sliceCopyFile', function() { before(function (done) { client.bucket.put({ Bucket: bucketName, ACL: acl }, function(err, data, res) { console.log('bucket created:', bucketName, err, data, res.statusCode) should.not.exist(err); res.should.have.status(200); done() }); }) describe('upload a big file', function() { before(function(done) { fs.open(tmpFilePath, 'w', (err, fd) => { if (err) return done(err); fs.ftruncate(fd, fileSizeInBytes, (err) => { if (err) return done(err); fs.close(fd, done); }); }); }); it('upload a big file by multipart upload', function(done) { this.timeout(0); client.config({ multipartUploadMinSize: 5 * 1024 * 1024 }); client.upload.start({ Bucket: bucketName, filePath: tmpFilePath, Key: key, headers }, function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }); }); }); describe('copy a big file', function() { before(function (done) { client.advance.sliceCopyFile({ SourceBucket: bucketName, SourceKey: key, DestinationObject: key + '-copy', DestinationBucket: bucketName, ACL: 'public-read', Headers: { 'x-kss-copy-source-if-unmodified-since': 'true', 'x-kss-copy-source-if-match': 'true' }, onProgress: function (progress) { if(progress % 10 === 0){ console.log('copy progress', progress + '%') } else if(progress === 100){ console.log('copy success!') } } }, function (err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }) }) after(function () { }); it('head copy file check', function(done) { client.object.head({ Bucket: bucketName, Key: key + '-copy', }, function (err, data, res) { should.not.exist(err); res.should.have.status(200); console.log('head copy file compare', res.headers) done(); }) }), it('after upload, test exist custom header', function (done) { client.config({ dataType: 'json' }); client.object.getObjectTagging({ Bucket: bucketName, Key: key + '-copy', }, function (err, data, res, body) { should.not.exist(err); res.should.have.status(200); let compareTag = testTagging(data.Tagging, headers['x-' + prefix + '-tagging']); compareTag.should.be.true; done(); }) }) it('delete big file', function (done) { client.object.del({ Bucket: bucketName, Key: key + '-copy', }, function (err, data, res) { should.not.exist(err); res.should.have.status(204); done(); }) }) it('delete copy file', function (done) { client.object.del({ Bucket: bucketName, Key: key }, function (err, data, res) { should.not.exist(err); res.should.have.status(204); done(); }) }) it('delete bucket', function (done) { setTimeout(() => { client.bucket.del(function(err, data, res) { should.not.exist(err); res.should.have.status(204); // 删除bucket,成功的状态码为204 done(); }) }, 1000); }) }) after(function (done) { fs.unlinkSync(tmpFilePath); done(); }) });