diff --git a/lib/browser/bucket.js b/lib/browser/bucket.js index 9ff7c2ca6..57ddae862 100644 --- a/lib/browser/bucket.js +++ b/lib/browser/bucket.js @@ -77,16 +77,6 @@ proto.getBucket = function getBucket() { return this.options.bucket; }; -proto.putBucket = async function putBucket(name, options) { - const params = this._bucketRequestParams('PUT', name, '', options); - params.successStatuses = [200]; - const result = await this.request(params); - return { - bucket: (result.headers.location && result.headers.location.substring(1)) || null, - res: result.res - }; -}; - proto.deleteBucket = async function deleteBucket(name, options) { const params = this._bucketRequestParams('DELETE', name, '', options); const result = await this.request(params); diff --git a/lib/browser/client.js b/lib/browser/client.js index 44c4379bc..b7f9a416e 100644 --- a/lib/browser/client.js +++ b/lib/browser/client.js @@ -16,7 +16,6 @@ const dateFormat = require('dateformat'); const bowser = require('bowser'); const signUtils = require('../common/signUtils'); const _isIP = require('../common/utils/isIP'); -const _checkBucketName = require('../common/utils/checkBucketName'); const _initOptions = require('../common/client/initOptions'); const globalHttpAgent = new AgentKeepalive(); @@ -102,15 +101,7 @@ merge(proto, require('./object')); // /** // * Bucket operations // */ -/** - * check Bucket Name - */ -proto._checkBucketName = function (name) { - if (!_checkBucketName(name)) { - throw new Error('The bucket must be conform to the specifications'); - } -}; // merge(proto, require('./bucket')); diff --git a/lib/bucket.js b/lib/bucket.js index 271a0cb5d..f406b7b0f 100644 --- a/lib/bucket.js +++ b/lib/bucket.js @@ -1,7 +1,6 @@ const assert = require('assert'); -const _checkBucketName = require('./common/utils/checkBucketName'); const proto = exports; @@ -17,16 +16,6 @@ function toArray(obj) { return [obj]; } -/** - * check Bucket Name - */ - -proto._checkBucketName = function (name) { - if (!_checkBucketName(name)) { - throw new Error('The bucket must be conform to the specifications'); - } -}; - /** * Bucket opertaions */ @@ -113,38 +102,6 @@ proto.getBucketInfo = async function getBucketInfo(name, options) { }; }; -proto.putBucket = async function putBucket(name, options) { - this._checkBucketName(name); - options = options || {}; - const params = this._bucketRequestParams('PUT', name, '', options); - - const startTag = '\n'; - const endTag = ''; - let paramlXML = ''; - - // server not support - // if (region) { - // paramlXML += `${region}`; - // params.content = `${startTag}${paramlXML}${endTag}`; - // } - - if (options.StorageClass) { - paramlXML += `${options.StorageClass}`; - } - - if (paramlXML) { - params.mime = 'xml'; - params.content = `${startTag}${paramlXML}${endTag}`; - } - - params.successStatuses = [200]; - const result = await this.request(params); - return { - bucket: (result.headers.location && result.headers.location.substring(1)) || null, - res: result.res - }; -}; - proto.deleteBucket = async function deleteBucket(name, options) { this._checkBucketName(name); const params = this._bucketRequestParams('DELETE', name, '', options); diff --git a/lib/common/bucket/_checkBucketName.js b/lib/common/bucket/_checkBucketName.js new file mode 100644 index 000000000..bddbbc622 --- /dev/null +++ b/lib/common/bucket/_checkBucketName.js @@ -0,0 +1,9 @@ +const checkBucketName = require('../utils/checkBucketName'); + +const proto = exports; + +proto._checkBucketName = function (name, createBucket) { + if (!checkBucketName(name, createBucket)) { + throw new Error('The bucket must be conform to the specifications'); + } +}; diff --git a/lib/common/bucket/index.js b/lib/common/bucket/index.js index 9fa19b897..59e8de83b 100644 --- a/lib/common/bucket/index.js +++ b/lib/common/bucket/index.js @@ -10,3 +10,5 @@ merge(proto, require('./deleteBucketEncryption')); merge(proto, require('./getBucketTags')); merge(proto, require('./putBucketTags')); merge(proto, require('./deleteBucketTags')); +merge(proto, require('./putBucket')); +merge(proto, require('./_checkBucketName')); diff --git a/lib/common/bucket/putBucket.js b/lib/common/bucket/putBucket.js new file mode 100644 index 000000000..86f54251d --- /dev/null +++ b/lib/common/bucket/putBucket.js @@ -0,0 +1,26 @@ +const proto = exports; +const obj2xml = require('../utils/obj2xml'); + +proto.putBucket = async function putBucket(name, options) { + this._checkBucketName(name, true); + options = options || {}; + const params = this._bucketRequestParams('PUT', name, '', options); + + const CreateBucketConfiguration = {}; + const paramlXMLObJ = { + CreateBucketConfiguration + }; + + if (options.StorageClass) { + CreateBucketConfiguration.StorageClass = options.StorageClass; + params.mime = 'xml'; + params.content = obj2xml(paramlXMLObJ, { headers: true }); + } + + params.successStatuses = [200]; + const result = await this.request(params); + return { + bucket: (result.headers.location && result.headers.location.substring(1)) || null, + res: result.res + }; +}; diff --git a/lib/common/utils/checkBucketName.js b/lib/common/utils/checkBucketName.js index b5d59466a..531ff21fc 100644 --- a/lib/common/utils/checkBucketName.js +++ b/lib/common/utils/checkBucketName.js @@ -2,8 +2,8 @@ * check Bucket Name */ -module.exports = function (name) { - const bucketRegex = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/; +module.exports = function (name, createBucket) { + const bucketRegex = createBucket ? /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/ : /^[a-z0-9_][a-z0-9-_]{1,61}[a-z0-9_]$/; const checkBucket = bucketRegex.test(name); return checkBucket; }; diff --git a/test/node/bucket.test.js b/test/node/bucket.test.js index d9cdf028b..af2e12bfc 100644 --- a/test/node/bucket.test.js +++ b/test/node/bucket.test.js @@ -83,11 +83,6 @@ describe('test/bucket.test.js', () => { const result1 = await store.putBucket(name); assert.equal(result1.bucket, name); assert.equal(result1.res.status, 200); - - // create a exists should work - const result2 = await store.putBucket(name); - assert.equal(result2.res.status, 200); - assert.equal(result2.bucket, name); }); it('should create an archive bucket', async () => { @@ -95,7 +90,6 @@ describe('test/bucket.test.js', () => { const result2 = await store.listBuckets(); const { buckets } = result2; const m = buckets.some(item => item.name === archvieBucket); - console.log(buckets); assert(m === true); buckets.map((item) => { if (item.name === archvieBucket) {