1
1
2
2
3
3
const assert = require ( 'assert' ) ;
4
+ const utils = require ( './common/utils/checkBucketname' ) ;
4
5
5
6
const proto = exports ;
6
7
@@ -16,6 +17,16 @@ function toArray(obj) {
16
17
return [ obj ] ;
17
18
}
18
19
20
+ /**
21
+ * check Bucket name
22
+ */
23
+
24
+ proto . _checkBucketName = function _checkBucketName ( name ) {
25
+ if ( ! utils . _checkBucketName ( name ) ) {
26
+ throw new Error ( 'The bucket must be conform to the specifications' ) ;
27
+ }
28
+ } ;
29
+
19
30
/**
20
31
* Bucket opertaions
21
32
*/
@@ -62,11 +73,11 @@ proto.listBuckets = async function listBuckets(query, options) {
62
73
} ;
63
74
64
75
proto . useBucket = function useBucket ( name ) {
65
- this . options . bucket = name ;
66
- return this ;
76
+ return this . setBucket ( name ) ;
67
77
} ;
68
78
69
79
proto . setBucket = function useBucket ( name ) {
80
+ this . _checkBucketName ( name ) ;
70
81
this . options . bucket = name ;
71
82
return this ;
72
83
} ;
@@ -76,6 +87,7 @@ proto.getBucket = function getBucket() {
76
87
} ;
77
88
78
89
proto . getBucketLocation = async function getBucketLocation ( name , options ) {
90
+ this . _checkBucketName ( name ) ;
79
91
name = name || this . getBucket ( ) ;
80
92
const params = this . _bucketRequestParams ( 'GET' , name , 'location' , options ) ;
81
93
params . successStatuses = [ 200 ] ;
@@ -88,6 +100,7 @@ proto.getBucketLocation = async function getBucketLocation(name, options) {
88
100
} ;
89
101
90
102
proto . getBucketInfo = async function getBucketInfo ( name , options ) {
103
+ this . _checkBucketName ( name ) ;
91
104
name = name || this . getBucket ( ) ;
92
105
const params = this . _bucketRequestParams ( 'GET' , name , 'bucketInfo' , options ) ;
93
106
params . successStatuses = [ 200 ] ;
@@ -100,6 +113,7 @@ proto.getBucketInfo = async function getBucketInfo(name, options) {
100
113
} ;
101
114
102
115
proto . putBucket = async function putBucket ( name , options ) {
116
+ this . _checkBucketName ( name ) ;
103
117
options = options || { } ;
104
118
const params = this . _bucketRequestParams ( 'PUT' , name , '' , options ) ;
105
119
@@ -131,6 +145,7 @@ proto.putBucket = async function putBucket(name, options) {
131
145
} ;
132
146
133
147
proto . deleteBucket = async function deleteBucket ( name , options ) {
148
+ this . _checkBucketName ( name ) ;
134
149
const params = this . _bucketRequestParams ( 'DELETE' , name , '' , options ) ;
135
150
const result = await this . request ( params ) ;
136
151
if ( result . status === 200 || result . status === 204 ) {
@@ -144,6 +159,7 @@ proto.deleteBucket = async function deleteBucket(name, options) {
144
159
// acl
145
160
146
161
proto . putBucketACL = async function putBucketACL ( name , acl , options ) {
162
+ this . _checkBucketName ( name ) ;
147
163
const params = this . _bucketRequestParams ( 'PUT' , name , 'acl' , options ) ;
148
164
params . headers = {
149
165
'x-oss-acl' : acl
@@ -157,6 +173,7 @@ proto.putBucketACL = async function putBucketACL(name, acl, options) {
157
173
} ;
158
174
159
175
proto . getBucketACL = async function getBucketACL ( name , options ) {
176
+ this . _checkBucketName ( name ) ;
160
177
const params = this . _bucketRequestParams ( 'GET' , name , 'acl' , options ) ;
161
178
params . successStatuses = [ 200 ] ;
162
179
params . xmlResponse = true ;
@@ -174,6 +191,7 @@ proto.getBucketACL = async function getBucketACL(name, options) {
174
191
// logging
175
192
176
193
proto . putBucketLogging = async function putBucketLogging ( name , prefix , options ) {
194
+ this . _checkBucketName ( name ) ;
177
195
const params = this . _bucketRequestParams ( 'PUT' , name , 'logging' , options ) ;
178
196
let xml = `${ '<?xml version="1.0" encoding="UTF-8"?>\n<BucketLoggingStatus>\n' +
179
197
'<LoggingEnabled>\n<TargetBucket>' } ${ name } </TargetBucket>\n`;
@@ -191,6 +209,7 @@ proto.putBucketLogging = async function putBucketLogging(name, prefix, options)
191
209
} ;
192
210
193
211
proto . getBucketLogging = async function getBucketLogging ( name , options ) {
212
+ this . _checkBucketName ( name ) ;
194
213
const params = this . _bucketRequestParams ( 'GET' , name , 'logging' , options ) ;
195
214
params . successStatuses = [ 200 ] ;
196
215
params . xmlResponse = true ;
@@ -204,6 +223,7 @@ proto.getBucketLogging = async function getBucketLogging(name, options) {
204
223
} ;
205
224
206
225
proto . deleteBucketLogging = async function deleteBucketLogging ( name , options ) {
226
+ this . _checkBucketName ( name ) ;
207
227
const params = this . _bucketRequestParams ( 'DELETE' , name , 'logging' , options ) ;
208
228
params . successStatuses = [ 204 , 200 ] ;
209
229
const result = await this . request ( params ) ;
@@ -215,6 +235,7 @@ proto.deleteBucketLogging = async function deleteBucketLogging(name, options) {
215
235
// website
216
236
217
237
proto . putBucketWebsite = async function putBucketWebsite ( name , config , options ) {
238
+ this . _checkBucketName ( name ) ;
218
239
// config: index, [error]
219
240
const params = this . _bucketRequestParams ( 'PUT' , name , 'website' , options ) ;
220
241
config = config || { } ;
@@ -235,6 +256,7 @@ proto.putBucketWebsite = async function putBucketWebsite(name, config, options)
235
256
} ;
236
257
237
258
proto . getBucketWebsite = async function getBucketWebsite ( name , options ) {
259
+ this . _checkBucketName ( name ) ;
238
260
const params = this . _bucketRequestParams ( 'GET' , name , 'website' , options ) ;
239
261
params . successStatuses = [ 200 ] ;
240
262
params . xmlResponse = true ;
@@ -247,6 +269,7 @@ proto.getBucketWebsite = async function getBucketWebsite(name, options) {
247
269
} ;
248
270
249
271
proto . deleteBucketWebsite = async function deleteBucketWebsite ( name , options ) {
272
+ this . _checkBucketName ( name ) ;
250
273
const params = this . _bucketRequestParams ( 'DELETE' , name , 'website' , options ) ;
251
274
params . successStatuses = [ 204 ] ;
252
275
const result = await this . request ( params ) ;
@@ -258,6 +281,7 @@ proto.deleteBucketWebsite = async function deleteBucketWebsite(name, options) {
258
281
// lifecycle
259
282
260
283
proto . putBucketLifecycle = async function putBucketLifecycle ( name , rules , options ) {
284
+ this . _checkBucketName ( name ) ;
261
285
// rules: [rule, ...]
262
286
// rule: [id], prefix, status, expiration, [days or date]
263
287
// status: 'Enabled' or 'Disabled'
@@ -287,6 +311,7 @@ proto.putBucketLifecycle = async function putBucketLifecycle(name, rules, option
287
311
} ;
288
312
289
313
proto . getBucketLifecycle = async function getBucketLifecycle ( name , options ) {
314
+ this . _checkBucketName ( name ) ;
290
315
const params = this . _bucketRequestParams ( 'GET' , name , 'lifecycle' , options ) ;
291
316
params . successStatuses = [ 200 ] ;
292
317
params . xmlResponse = true ;
@@ -317,6 +342,7 @@ proto.getBucketLifecycle = async function getBucketLifecycle(name, options) {
317
342
} ;
318
343
319
344
proto . deleteBucketLifecycle = async function deleteBucketLifecycle ( name , options ) {
345
+ this . _checkBucketName ( name ) ;
320
346
const params = this . _bucketRequestParams ( 'DELETE' , name , 'lifecycle' , options ) ;
321
347
params . successStatuses = [ 204 ] ;
322
348
const result = await this . request ( params ) ;
@@ -326,6 +352,7 @@ proto.deleteBucketLifecycle = async function deleteBucketLifecycle(name, options
326
352
} ;
327
353
328
354
proto . putBucketCORS = async function putBucketCORS ( name , rules , options ) {
355
+ this . _checkBucketName ( name ) ;
329
356
rules = rules || [ ] ;
330
357
assert ( rules . length , 'rules is required' ) ;
331
358
rules . forEach ( ( rule ) => {
@@ -371,6 +398,7 @@ proto.putBucketCORS = async function putBucketCORS(name, rules, options) {
371
398
} ;
372
399
373
400
proto . getBucketCORS = async function getBucketCORS ( name , options ) {
401
+ this . _checkBucketName ( name ) ;
374
402
const params = this . _bucketRequestParams ( 'GET' , name , 'cors' , options ) ;
375
403
params . successStatuses = [ 200 ] ;
376
404
params . xmlResponse = true ;
@@ -394,6 +422,7 @@ proto.getBucketCORS = async function getBucketCORS(name, options) {
394
422
} ;
395
423
396
424
proto . deleteBucketCORS = async function deleteBucketCORS ( name , options ) {
425
+ this . _checkBucketName ( name ) ;
397
426
const params = this . _bucketRequestParams ( 'DELETE' , name , 'cors' , options ) ;
398
427
params . successStatuses = [ 204 ] ;
399
428
const result = await this . request ( params ) ;
@@ -405,6 +434,7 @@ proto.deleteBucketCORS = async function deleteBucketCORS(name, options) {
405
434
// referer
406
435
407
436
proto . putBucketReferer = async function putBucketReferer ( name , allowEmpty , referers , options ) {
437
+ this . _checkBucketName ( name ) ;
408
438
const params = this . _bucketRequestParams ( 'PUT' , name , 'referer' , options ) ;
409
439
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n<RefererConfiguration>\n' ;
410
440
xml += ` <AllowEmptyReferer>${ allowEmpty ? 'true' : 'false' } </AllowEmptyReferer>\n` ;
@@ -428,6 +458,7 @@ proto.putBucketReferer = async function putBucketReferer(name, allowEmpty, refer
428
458
} ;
429
459
430
460
proto . getBucketReferer = async function getBucketReferer ( name , options ) {
461
+ this . _checkBucketName ( name ) ;
431
462
const params = this . _bucketRequestParams ( 'GET' , name , 'referer' , options ) ;
432
463
params . successStatuses = [ 200 ] ;
433
464
params . xmlResponse = true ;
@@ -446,6 +477,7 @@ proto.getBucketReferer = async function getBucketReferer(name, options) {
446
477
} ;
447
478
448
479
proto . deleteBucketReferer = async function deleteBucketReferer ( name , options ) {
480
+ this . _checkBucketName ( name ) ;
449
481
return await this . putBucketReferer ( name , true , null , options ) ;
450
482
} ;
451
483
0 commit comments