ks3
Version:
本代码库为`金山云存储KS3`服务.主要提供`KS3 nodejs SDK`和`命令行工具`.
517 lines (487 loc) • 18.3 kB
JavaScript
var KS3 = require('../..')
var should = require('should')
require('should-http')
const { ak: AK, sk: SK } = require('../config')
let Endpoint = 'ks3-cn-qingdao.ksyuncs.com'
let testParmas = {
Endpoint,
}
var ak = AK
var sk = SK
var bucketName = 'test-sdk-zwy-qingdao'
var client = new KS3(ak, sk, bucketName)
/**
* 不适用于对自定义域名的测试
*/
describe('API service', function () {
describe('service api', function () {
it('should get a 2XX statuscode ', function (done) {
client.service.get(testParmas, function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
})
})
})
})
describe('API bucket', function () {
var acl = 'public-read'
before(function (done) {
client.bucket.put(
{
Bucket: bucketName,
ACL: acl,
Endpoint,
},
function (err, data, res) {
console.log(
'bucket created:',
bucketName,
err,
data,
)
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
describe('create bucket', function () {
it('require ACL not pass verify', function () {
;(function () {
client.bucket.put(
{
Bucket: bucketName,
ACL: 'testacl',
Endpoint,
},
function () {}
)
}).should.throw('the illegal acl')
})
})
describe('other handle for bucket', function () {
describe('bucket ACL', function (done) {
it('Put ACL && get ACL', function (done) {
client.bucket.putACL(
{ ACL: acl, Endpoint },
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
client.bucket.getACL(testParmas, function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
})
}
)
})
})
describe('bucket location', function () {
it('get location should get a 2XX statuscode', function (done) {
client.bucket.getLocation(
testParmas,
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
})
// NOTICE: need main account ak/sk
describe('bucket logging', function () {
it('put logging should get a 2XX statusCode', function (done) {
client.bucket.putLogging(
{
Endpoint,
Logging: {
targetBucket,
},
},
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
it('get logging should get a 2XX statuscode', function (done) {
client.bucket.getLogging(testParmas, function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
})
})
it('close logging should get a 2XX statuscode', function (done) {
client.bucket.putLogging(testParmas, function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
})
})
})
describe('list objects', function () {
it('get objects without params', function (done) {
this.timeout(300000)
client.bucket.get(testParmas, function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
})
})
it('get objects with params', function (done) {
client.bucket.get(
{
MaxKeys: 30,
Marker: '',
Prefix: '',
Endpoint,
},
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
})
describe('head bucket', function () {
it('should get a 2XX statuscode for head bucket', function (done) {
client.bucket.head(
{
Bucket: bucketName,
Endpoint,
},
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
})
describe('bucket policy', function () {
it('set bucket error policy', function (done) {
client.bucket.putBucketPolicy(
{
Endpoint,
Policy: {
Version: '2008-10-17',
Statement: [
{
Sid: '1',
Effect: 'Allow',
Principal: {
KSC: ['krn:ksc:iam::accountID:root'],
},
Action: ['ks3:*'],
Resource: [
'krn:ksc:ks3:::ks3-example',
'krn:ksc:ks3:::ks3-example/*',
],
Condition: {
IpAddress: {
'ksc:SourceIp': '54.240.143.1',
},
},
},
],
},
},
function (err, data, res) {
err.should.have.property('statusCode', 400)
done()
}
)
})
it('get bucket error policy', function (done) {
client.bucket.getBucketPolicy(
testParmas,
function (err, data, res) {
err.should.have.property('statusCode', 404)
done()
}
)
})
it('set bucket correct policy', function (done) {
const policy = {
Statement: [
{
Resource: [
'krn:ksc:ks3:::' + bucketName,
'krn:ksc:ks3:::' + bucketName + '/*',
],
Principal: {
KSC: ['*'],
},
Action: ['ks3:*'],
Effect: 'Allow',
},
],
}
client.bucket.putBucketPolicy(
{
Policy: policy,
Endpoint,
},
function (err, data, res) {
res.should.have.status(204)
done()
}
)
})
it('get bucket policy', function (done) {
client.bucket.getBucketPolicy(
testParmas,
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
it('delete bucket policy', function (done) {
client.bucket.deleteBucketPolicy(
testParmas,
function (err, data, res) {
res.should.have.status(204)
done()
}
)
})
})
describe('bucket lifecycle', function () {
it('set bucket lifecycle', function (done) {
client.bucket.putBucketLifecycle(
{
Endpoint,
Rules: [
{
id: 'id1',
filter: {
prefix: 'documents',
},
expiration: {
date: '2016-12-31T00:00:00+08:00',
},
status: 'Enabled',
},
],
},
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
it('get bucket lifecycle', function (done) {
client.bucket.getBucketLifecycle(
testParmas,
function (err, data, res) {
should.equal(err, null)
res.should.have.status(200)
done()
}
)
})
it('delete bucket lifecycle', function (done) {
client.bucket.deleteBucketLifecycle(
testParmas,
function (err, data, res) {
should.equal(err, null)
res.should.have.status(204)
done()
}
)
})
})
describe('bucket CORS', function () {
it('check bucket cors legal', function () {
;(function () {
client.bucket.putBucketCors(
{
allowedMethod: '*',
Endpoint,
},
function () {}
)
}).should.throw()
})
it('set bucket cors', function (done) {
client.bucket.putBucketCors(
{
Rules: [
{
allowedMethod: ['POST', 'GET'],
allowedOrigin: [
'http://ks3.console.ksyun.com',
'http://ks3.ksyun.com',
],
allowedHeader: [
'x-kss-test1:nodesdk-test1',
'x-kss-test2:nodesdk-test2',
],
maxAgeSeconds: 3000,
exposeHeader:
'x-kss-test1:nodesdk-test1, x-kss-test2:nodesdk-test2',
},
],
Endpoint,
},
function (err, data, res) {
should.equal(err, null)
res.should.have.status(200)
done()
}
)
})
it('get bucket cors', function (done) {
client.bucket.getBucketCors(
testParmas,
function (err, data, res) {
res.should.have.status(200)
done()
}
)
})
it('delete bucket cors', function (done) {
client.bucket.deleteBucketCors(
testParmas,
function (err, data, res) {
res.should.have.status(204)
done()
}
)
})
})
describe('bucket mirror', function () {
this.timeout(10000)
it('set bucket mirror', function (done) {
client.bucket.putBucketMirror(
{
Mirror: {
version: 'V3', // 回源类型,目前只支持V3
use_default_robots: false, // 是否使用默认的robots.txt
sync_mirror_rules: [
// 一组同步回源规则,最多可配置20个。该字段与async_mirror_rule必须至少有一个,可同时存在。
{
mirror_url:
'http://v-ks-a-i.originalvod.com', // 源站url,必须以http或者https开头
saving_setting: {
// 从源站回源得到的文件在上传ks3时的配置。
acl: 'private',
},
},
],
},
Endpoint,
},
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
it('get bucket mirror', function (done) {
client.bucket.getBucketMirror(
testParmas,
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
it('delete bucket mirror', function (done) {
client.bucket.deleteBucketMirror(
testParmas,
function (err, data, res) {
should.not.exist(err)
res.should.have.status(204)
done()
}
)
})
})
// getACLType
describe('bucket acl', function () {
it('bucket acl', function (done) {
client.config({
dataType: 'json',
})
client.bucket.getACLType(testParmas, function (err, data, res) {
should.not.exist(err)
should.equal(data, acl)
res.should.have.status(200)
done()
})
})
})
// listMultipartUploads
describe('bucket listMultipartUploads', function () {
it('bucket listMultipartUploads', function (done) {
client.bucket.listMultipartUploads(
testParmas,
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
})
describe('bucket replication configuration', function () {
it('set bucket replication config', function (done) {
this.timeout(0)
setTimeout(function () {
client.bucket.putBucketReplicationConfiguration(
{
Endpoint,
Rule: {
prefix: ['abc', 'xyz'],
deleteMarkerStatus: 'Disabled',
targetBucket,
historicalObjectReplication: 'Enabled',
},
},
function (err, data, res) {
should.equal(err, null)
res.should.have.status(200)
done()
}
)
}, 5000)
})
it('get bucket replication config', function (done) {
client.bucket.getBucketReplicationConfiguration(
testParmas,
function (err, data, res) {
should.equal(err, null)
res.should.have.status(200)
done()
}
)
})
it('delete bucket replication config', function (done) {
client.bucket.deleteBucketReplicationConfiguration(
testParmas,
function (err, data, res) {
should.not.exist(err)
res.should.have.status(200)
done()
}
)
})
})
})
after(function (done) {
client.bucket.del(testParmas, function (err, data, res) {
should.not.exist(err)
res.should.have.status(204) // 删除bucket,成功的状态码为204
done()
})
})
})