ks3
Version:
本代码库为`金山云存储KS3`服务.主要提供`KS3 nodejs SDK`和`命令行工具`.
248 lines (221 loc) • 6.16 kB
JavaScript
var KS3 = require('..');
var should = require('should');
require('should-http');
var path = require('path');
var fs = require('fs');
const { ak, sk } = require('./config')
var bigFile = process.env.BIGFILE || path.join(__dirname, './assets/风之万里,黎明之空.txt');
var updir = process.env.UPDIR || path.join(__dirname, './assets/updir/');
var debug = require('debug')('test-upload');
var bucketName = 'test-nodesdk-' + (Date.now())
var client = new KS3(ak, sk, bucketName);
describe('upload module', function () {
this.timeout(10000);
before (function (done) {
client.bucket.put({
Bucket: bucketName
}, function (err, data, res){
console.log(err)
should.not.exist(err)
done()
})
})
describe('upload a file', function() {
it('upload a object with file content', function(done) {
this.timeout(300000);
var filePath = bigFile;
var fileName = (function(){
var s = filePath.split('/');
return s[s.length-1];
})();
var key = 'test_upload_file_from_upload_'+fileName;
client.upload.start({
Bucket: bucketName,
FilePath: filePath,
Key: key
},
function(err, data, res) {
should.not.exist(err);
res.should.have.status(200);
done(); //对于回调链来说done实际上意味着告诉mocha从此处开始测试,一层层回调回去。
});
});
it('upload a file and set content-type', function(done) {
var filePath = path.join(__dirname, './assets/test_content_type.html');
var fileName = (function(){
var s = filePath.split('/');
return s[s.length-1];
})();
var key = 'test_upload_file_from_upload_and_set_content_type_'+fileName;
client.upload.start({
Bucket: bucketName,
FilePath: filePath,
Key: key
},
function(err, data, res) {
should.not.exist(err);
res.should.have.status(200);
done();
});
});
});
describe('upload a file and set headers', function () {
var filePath = path.join(__dirname, './assets/test_content_type.html');
var fileName = (function(){
var s = filePath.split('/');
return s[s.length-1];
})();
var key = 'test_upload_file_from_upload_and_set_content_type_'+fileName;
before(function (done) {
client.upload.start({
Bucket: bucketName,
FilePath: filePath,
Key: key,
Headers: {
'x-kss-meta-tag': 'test'
}
},
function(err, data, res) {
should.not.exist(err);
res.should.have.status(200);
done();
});
})
it('after upload, test exist custom header', function (done) {
client.object.head({
Bucket: bucketName,
Key: key
}, function (err, data, res) {
should.not.exist(err);
res.should.have.status(200);
var tag = res.headers['x-kss-meta-tag']
tag.should.equal('test');
done();
})
})
after(function (done) {
client.object.del({
Bucket: bucketName,
Key: key
}, function (err, data, res) {
should.not.exist(err);
res.should.have.status(204);
done();
})
})
})
describe('test multipart upload', function() {
var tmpFilePath = path.join(__dirname, './assets/bigFileOver100M.tmp')
before(function() {
/**
* 创建一个大于100M的文件
*/
var mock_data = '金山云CDN服务将用户内容分发到全球700余个边缘节点,提高用户访问网站的响应速度与网站的可用性,通过智能监控实时调度异常节点资源,保证全局性能和可用性, 控制台可以帮助用户完成新增CDN加速域名、刷新/预加载缓存目录、URL等配置任务,并提供数据分析等统计图表。本文档主要介绍CDN控制台操作手册。';
mock_data = Buffer.from(new Array(30).join(mock_data));
var unit_size = mock_data.length;
var total = 6 * 1024* 1024; //5 MB
var sumSize = 0;
while(sumSize < total) {
fs.appendFileSync(tmpFilePath, mock_data);
sumSize += unit_size;
debug('sumSize: ' + sumSize);
}
});
it('upload a big file by multipart upload', function(done) {
this.timeout(900000);
client.config({
multipartUploadMinSize: 5* 1024*1024
});
var key = 'multipartUpload/bigFile.tmp';
client.upload.start({
Bucket: bucketName,
FilePath: tmpFilePath,
Key: key
},
function(err, data, res) {
should.not.exist(err);
res.should.have.status(200);
done();
});
});
after(function() {
//删除临时文件
fs.unlinkSync(tmpFilePath);
});
});
describe('upload a directory', function() {
it('upload a directory without subdirectory', function(done) {
var filePath = updir;
var key = 'test_upload_directory_without_subdir';
client.upload.start({
Bucket: bucketName,
FilePath: filePath,
Key: key,
fileSetting:{
isDeep:false,
ignore:/(.(swp|ds_store)$)/ig
},
ACL:'public-read'
},
function(err, data, res) {
should.not.exist(err);
res.should.have.status(200);
done();
});
});
it('upload a directory with subdirectory', function(done) {
var filePath = updir;
var key = 'test_upload_directory_with_subdir';
client.upload.start({
Bucket: bucketName,
FilePath: filePath,
Key: key,
fileSetting:{
isDeep:true,
ignore:/(.(swp|ds_store)$)/ig
}
},
function(err, data, res) {
should.not.exist(err);
res.should.have.status(200);
done();
});
});
});
after(function (done) {
client.config({
dataType: 'json'
});
this.timeout(30000)
// 获取bucket数据 --> 循环删除object --> 删除bucket
client.bucket.get(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
}, function (err, data, res) {
if (err) {
reject(err)
}
resolve()
})
})
promiseQueue.push(p)
})
Promise.all(promiseQueue).then(val => {
client.bucket.del(function (err, data, res) {
should.not.exist(err)
res.should.have.status(204)
done()
})
}).catch(err => {
done()
})
})
});
})