ks3-js-sdk
Version:
js sdk for ks3
303 lines • 12.8 kB
JavaScript
var uploadPost = function() {
describe('post方法上传文件', function () {
var key = 'demo.txt';
var bucket = '';
// 支持上传文件
// 支持上传字符串
// 表单中必须有file
var file = blob({ size: 1024 * 1024 * 5 });
var str = 'abcdef';
var now = Math.round(Date.now() / 1000);
var exp = now + 900;
describe('上传文件,不设置acl', function () {
it('上传文件,不设置acl', function (done) {
this.timeout(5000);
let formData = new FormData();
let policy = {
expiration: new Date(exp * 1000).toISOString(),
conditions: [
["eq", "$bucket", bucket || ks3.config.bucket],
['eq', '$key', key]
]
}
formData.append('key', key);
formData.append('policy', JSON.stringify(policy));
formData.append('file', file);
ks3.postObject({formData}).then(res => {
should.not.exist(res.error);
res.statusCode.should.be.equal(204);
done();
})
})
it('获取该文件的ACL', function (done) {
this.timeout(2000);
ks3.getObjectAcl({
key
}).then(res => {
let grant = res.body.AccessControlPolicy.AccessControlList.Grant;
grant.Permission.should.be.equal('FULL_CONTROL');
should.not.exist(res.error);
res.statusCode.should.be.equal(200);
done();
})
})
it('删除该文件', function (done) {
this.timeout(2000);
ks3.delObject({
key
}).then(res => {
should.not.exist(res.error);
res.statusCode.should.be.equal(204);
done();
})
})
})
describe('上传字符串,不设置acl', function () {
it('上传字符串,不设置acl', function (done) {
this.timeout(5000);
let formData = new FormData();
let policy = {
expiration: new Date(exp * 1000).toISOString(),
conditions: [
["eq", "$bucket", bucket || ks3.config.bucket],
['eq', '$key', key],
['eq', '$x-kss-server-side-encryption', 'AES256']
]
}
formData.append('key', key);
formData.append('policy', JSON.stringify(policy));
formData.append('file', str);
formData.append('x-kss-server-side-encryption', 'AES256');
ks3.postObject({formData}).then(res => {
should.not.exist(res.error);
res.statusCode.should.be.equal(204);
done();
})
})
it('获取该文件的ACL', function (done) {
this.timeout(2000);
ks3.getObjectAcl({
key
}).then(res => {
let grant = res.body.AccessControlPolicy.AccessControlList.Grant;
grant.Permission.should.be.equal('FULL_CONTROL');
should.not.exist(res.error);
res.statusCode.should.be.equal(200);
done();
})
})
it('通过Range请求头获取该文件的一部分', function (done) {
this.timeout(2000);
ks3.getObject({
key,
headers: {
Range: 'bytes=0-2'
}
}).then(res => {
res.headers['content-length'].should.be.equal('3');
should.not.exist(res.error);
res.statusCode.should.be.equal(206);
done();
})
})
it('通过range参数获取该文件的一部分', function (done) {
this.timeout(2000);
ks3.getObject({
key,
range: 'bytes=0-2'
}).then(res => {
res.headers['content-length'].should.be.equal('3');
should.not.exist(res.error);
res.statusCode.should.be.equal(206);
done();
})
})
it('删除该文件', function (done) {
this.timeout(2000);
ks3.delObject({
key
}).then(res => {
should.not.exist(res.error);
res.statusCode.should.be.equal(204);
done();
})
})
})
describe('上传文件,通过encryption表单项设置服务端加密', function () {
it('上传文件,通过encryption表单项设置服务端加密', function (done) {
this.timeout(5000);
let formData = new FormData();
let policy = {
expiration: new Date(exp * 1000).toISOString(),
conditions: [
["eq", "$bucket", bucket || ks3.config.bucket],
['eq', '$key', key],
['eq', '$x-kss-server-side-encryption', 'AES256']
]
}
formData.append('key', key);
formData.append('policy', JSON.stringify(policy));
formData.append('file', file);
formData.append('x-kss-server-side-encryption', 'AES256');
ks3.postObject({formData}).then(res => {
should.not.exist(res.error);
res.statusCode.should.be.equal(204);
done();
})
})
it('获取该文件的ACL', function (done) {
this.timeout(2000);
ks3.getObjectAcl({
key
}).then(res => {
let grant = res.body.AccessControlPolicy.AccessControlList.Grant;
grant.Permission.should.be.equal('FULL_CONTROL');
should.not.exist(res.error);
res.statusCode.should.be.equal(200);
done();
})
})
it('删除该文件', function (done) {
this.timeout(2000);
ks3.delObject({
key
}).then(res => {
should.not.exist(res.error);
res.statusCode.should.be.equal(204);
done();
})
})
})
describe('上传文件,设置acl为private,并通过请求头设置存储类型为STANDARD_IA', function () {
it('上传文件,设置acl为private,并通过请求头设置存储类型为STANDARD_IA', function (done) {
this.timeout(5000);
let formData = new FormData();
let policy = {
expiration: new Date(exp * 1000).toISOString(),
conditions: [
["eq", "$bucket", bucket || ks3.config.bucket],
['eq', '$key', key],
['eq', '$acl', 'private'],
['eq', '$x-kss-storage-class', 'STANDARD_IA'],
]
}
formData.append('key', key);
formData.append('policy', JSON.stringify(policy));
formData.append('file', file);
formData.append('acl', 'private');
formData.append('x-kss-storage-class', 'STANDARD_IA');
ks3.postObject({formData}).then(res => {
should.not.exist(res.error);
res.statusCode.should.be.equal(204);
done();
})
})
it('获取该文件的ACL', function (done) {
this.timeout(2000);
ks3.getObjectAcl({
key
}).then(res => {
let grant = res.body.AccessControlPolicy.AccessControlList.Grant;
grant.Permission.should.be.equal('FULL_CONTROL');
should.not.exist(res.error);
res.statusCode.should.be.equal(200);
done();
})
})
it('获取文件的存储类型', function (done) {
this.timeout(2000);
ks3.listObjects({
prefix: key
}).then(res => {
let contents = res.body.ListBucketResult.Contents;
let object;
if (contents instanceof Array) {
object = contents.find((item) => item.Key === key);
} else {
object = contents;
}
object.StorageClass.should.be.equal('STANDARD_IA');
should.not.exist(res.error);
res.statusCode.should.be.equal(200);
done();
})
})
it('删除该文件', function (done) {
this.timeout(2000);
ks3.delObject({
key
}).then(res => {
should.not.exist(res.error);
res.statusCode.should.be.equal(204);
done();
})
})
})
describe('上传文件,设置acl为public-read,并通过请求头设置存储类型为ARCHIVE', function () {
it('上传文件,设置acl为public-read,并通过请求头设置存储类型为ARCHIVE', function (done) {
this.timeout(5000);
let formData = new FormData();
let policy = {
expiration: new Date(exp * 1000).toISOString(),
conditions: [
["eq", "$bucket", bucket || ks3.config.bucket],
['eq', '$key', key],
['eq', '$acl', 'public-read'],
['eq', '$x-kss-storage-class', 'ARCHIVE'],
]
}
formData.append('key', key);
formData.append('policy', JSON.stringify(policy));
formData.append('file', file);
formData.append('acl', 'public-read');
formData.append('x-kss-storage-class', 'ARCHIVE');
ks3.postObject({formData}).then(res => {
should.not.exist(res.error);
res.statusCode.should.be.equal(204);
done();
})
})
it('获取该文件的ACL', function (done) {
this.timeout(2000);
ks3.getObjectAcl({
key
}).then(res => {
let grant = res.body.AccessControlPolicy.AccessControlList.Grant;
grant[0].Permission.should.be.equal('FULL_CONTROL');
grant[1].Permission.should.be.equal('READ');
should.not.exist(res.error);
res.statusCode.should.be.equal(200);
done();
})
})
it('获取文件的存储类型', function (done) {
this.timeout(2000);
ks3.listObjects({
prefix: key
}).then(res => {
let contents = res.body.ListBucketResult.Contents;
let object;
if (contents instanceof Array) {
object = contents.find((item) => item.Key === key);
} else {
object = contents;
}
object.StorageClass.should.be.equal('ARCHIVE');
should.not.exist(res.error);
res.statusCode.should.be.equal(200);
done();
})
})
it('删除该文件', function (done) {
this.timeout(2000);
ks3.delObject({
key
}).then(res => {
should.not.exist(res.error);
res.statusCode.should.be.equal(204);
done();
})
})
})
})
}