-
Notifications
You must be signed in to change notification settings - Fork 174
/
BucketController.java
472 lines (450 loc) · 17.4 KB
/
BucketController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* Copyright 2017-2024 Adobe.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adobe.testing.s3mock;
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.X_AMZ_BUCKET_OBJECT_LOCK_ENABLED;
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.X_AMZ_OBJECT_OWNERSHIP;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.CONTINUATION_TOKEN;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.ENCODING_TYPE;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.KEY_MARKER;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.LIFECYCLE;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.LIST_TYPE_V2;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.LOCATION;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.MAX_KEYS;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.NOT_LIFECYCLE;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.NOT_LIST_TYPE;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.NOT_LOCATION;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.NOT_OBJECT_LOCK;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.NOT_UPLOADS;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.NOT_VERSIONS;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.OBJECT_LOCK;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.START_AFTER;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.VERSIONS;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.VERSION_ID_MARKER;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
import com.adobe.testing.s3mock.dto.BucketLifecycleConfiguration;
import com.adobe.testing.s3mock.dto.ListAllMyBucketsResult;
import com.adobe.testing.s3mock.dto.ListBucketResult;
import com.adobe.testing.s3mock.dto.ListBucketResultV2;
import com.adobe.testing.s3mock.dto.ListVersionsResult;
import com.adobe.testing.s3mock.dto.LocationConstraint;
import com.adobe.testing.s3mock.dto.ObjectLockConfiguration;
import com.adobe.testing.s3mock.service.BucketService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.model.ObjectOwnership;
/**
* Handles requests related to buckets.
*/
@CrossOrigin(origins = "*", exposedHeaders = "*")
@Controller
@RequestMapping("${com.adobe.testing.s3mock.contextPath:}")
public class BucketController {
private final BucketService bucketService;
private final Region region;
public BucketController(BucketService bucketService, Region region) {
this.bucketService = bucketService;
this.region = region;
}
//================================================================================================
// /
//================================================================================================
/**
* List all existing buckets.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListBuckets.html">API Reference</a>
*
* @return List of all Buckets
*/
@GetMapping(
value = "/",
produces = APPLICATION_XML_VALUE
)
public ResponseEntity<ListAllMyBucketsResult> listBuckets() {
var listAllMyBucketsResult = bucketService.listBuckets();
return ResponseEntity.ok(listAllMyBucketsResult);
}
//================================================================================================
// /{bucketName:.+}
//================================================================================================
/**
* Create a bucket if the name matches a simplified version of the bucket naming rules.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html">API Reference Bucket Naming</a>
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html">API Reference</a>
*
* @param bucketName name of the bucket that should be created.
*
* @return 200 OK if creation was successful.
*/
@PutMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
NOT_OBJECT_LOCK,
NOT_LIFECYCLE
}
)
public ResponseEntity<Void> createBucket(@PathVariable final String bucketName,
@RequestHeader(value = X_AMZ_BUCKET_OBJECT_LOCK_ENABLED,
required = false, defaultValue = "false") boolean objectLockEnabled,
@RequestHeader(value = X_AMZ_OBJECT_OWNERSHIP,
required = false, defaultValue = "BucketOwnerEnforced") ObjectOwnership objectOwnership) {
bucketService.verifyBucketNameIsAllowed(bucketName);
bucketService.verifyBucketDoesNotExist(bucketName);
bucketService.createBucket(bucketName, objectLockEnabled, objectOwnership);
return ResponseEntity.ok().build();
}
/**
* Check if a bucket exists.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadBucket.html">API Reference</a>
*
* @param bucketName name of the Bucket.
*
* @return 200 if it exists; 404 if not found.
*/
@RequestMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
method = RequestMethod.HEAD
)
public ResponseEntity<Void> headBucket(@PathVariable final String bucketName) {
bucketService.verifyBucketExists(bucketName);
//return bucket region
//return bucket location
return ResponseEntity.ok().build();
}
/**
* Delete a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucket.html">API Reference</a>
*
* @param bucketName name of the Bucket.
*
* @return 204 if Bucket was deleted; 404 if not found
*/
@DeleteMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
NOT_LIFECYCLE
}
)
public ResponseEntity<Void> deleteBucket(@PathVariable String bucketName) {
bucketService.verifyBucketExists(bucketName);
bucketService.verifyBucketIsEmpty(bucketName);
bucketService.deleteBucket(bucketName);
return ResponseEntity.noContent().build();
}
/**
* Get ObjectLockConfiguration of a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectLockConfiguration.html">API Reference</a>
*
* @param bucketName name of the Bucket.
*
* @return 200, ObjectLockConfiguration
*/
@GetMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
OBJECT_LOCK,
NOT_LIST_TYPE
},
produces = APPLICATION_XML_VALUE
)
public ResponseEntity<ObjectLockConfiguration> getObjectLockConfiguration(
@PathVariable String bucketName) {
bucketService.verifyBucketExists(bucketName);
var configuration = bucketService.getObjectLockConfiguration(bucketName);
return ResponseEntity.ok(configuration);
}
/**
* Put ObjectLockConfiguration of a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObjectLockConfiguration.html">API Reference</a>
*
* @param bucketName name of the Bucket.
*
* @return 200, ObjectLockConfiguration
*/
@PutMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
OBJECT_LOCK
},
consumes = APPLICATION_XML_VALUE
)
public ResponseEntity<Void> putObjectLockConfiguration(
@PathVariable String bucketName,
@RequestBody ObjectLockConfiguration configuration) {
bucketService.verifyBucketExists(bucketName);
bucketService.setObjectLockConfiguration(bucketName, configuration);
return ResponseEntity.ok().build();
}
/**
* Get BucketLifecycleConfiguration of a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLifecycleConfiguration.html">API Reference</a>
*
* @param bucketName name of the Bucket.
*
* @return 200, ObjectLockConfiguration
*/
@GetMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
LIFECYCLE,
NOT_LIST_TYPE
},
produces = APPLICATION_XML_VALUE
)
public ResponseEntity<BucketLifecycleConfiguration> getBucketLifecycleConfiguration(
@PathVariable String bucketName) {
bucketService.verifyBucketExists(bucketName);
var configuration = bucketService.getBucketLifecycleConfiguration(bucketName);
return ResponseEntity.ok(configuration);
}
/**
* Put BucketLifecycleConfiguration of a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketLifecycleConfiguration.html">API Reference</a>
*
* @param bucketName name of the Bucket.
*
* @return 200, ObjectLockConfiguration
*/
@PutMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
LIFECYCLE
},
consumes = APPLICATION_XML_VALUE
)
public ResponseEntity<Void> putBucketLifecycleConfiguration(
@PathVariable String bucketName,
@RequestBody BucketLifecycleConfiguration configuration) {
bucketService.verifyBucketExists(bucketName);
bucketService.setBucketLifecycleConfiguration(bucketName, configuration);
return ResponseEntity.ok().build();
}
/**
* Delete BucketLifecycleConfiguration of a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketLifecycle.html">API Reference</a>
*
* @param bucketName name of the Bucket.
*
* @return 200, ObjectLockConfiguration
*/
@DeleteMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
LIFECYCLE
}
)
public ResponseEntity<Void> deleteBucketLifecycleConfiguration(
@PathVariable String bucketName) {
bucketService.verifyBucketExists(bucketName);
bucketService.deleteBucketLifecycleConfiguration(bucketName);
return ResponseEntity.noContent().build();
}
/**
* Get location of a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLocation.html">API Reference</a>
*
* @param bucketName name of the Bucket.
*
* @return 200, LocationConstraint
*/
@GetMapping(
value = "/{bucketName:.+}",
params = {
LOCATION
}
)
public ResponseEntity<LocationConstraint> getBucketLocation(
@PathVariable String bucketName) {
bucketService.verifyBucketExists(bucketName);
return ResponseEntity.ok(new LocationConstraint(region));
}
/**
* Retrieve list of objects of a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjects.html">API Reference</a>
*
* @param bucketName {@link String} set bucket name
* @param prefix {@link String} find object names they start with prefix
* @param encodingType whether to use URL encoding (encodingtype="url") or not
*
* @return {@link ListBucketResult} a list of objects in Bucket
* @deprecated Long since replaced by ListObjectsV2, {@see #listObjectsInsideBucketV2}
*/
@GetMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
NOT_UPLOADS,
NOT_OBJECT_LOCK,
NOT_LIST_TYPE,
NOT_LIFECYCLE,
NOT_LOCATION,
NOT_VERSIONS
},
produces = APPLICATION_XML_VALUE
)
@Deprecated(since = "2.12.2", forRemoval = true)
public ResponseEntity<ListBucketResult> listObjects(
@PathVariable String bucketName,
@RequestParam(required = false) String prefix,
@RequestParam(required = false) String delimiter,
@RequestParam(required = false) String marker,
@RequestParam(name = ENCODING_TYPE, required = false) String encodingType,
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys) {
bucketService.verifyBucketExists(bucketName);
bucketService.verifyMaxKeys(maxKeys);
bucketService.verifyEncodingType(encodingType);
var listBucketResult = bucketService.listObjectsV1(bucketName, prefix, delimiter,
marker, encodingType, maxKeys);
return ResponseEntity.ok(listBucketResult);
}
/**
* Retrieve list of objects of a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html">API Reference</a>
*
* @param bucketName {@link String} set bucket name
* @param prefix {@link String} find object names they start with prefix
* @param startAfter {@link String} return key names after a specific object key in your key
* space
* @param maxKeys {@link Integer} set maximum number of keys to be returned
* @param continuationToken {@link String} pagination token returned by previous request
*
* @return {@link ListBucketResultV2} a list of objects in Bucket
*/
@GetMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
LIST_TYPE_V2
},
produces = APPLICATION_XML_VALUE
)
public ResponseEntity<ListBucketResultV2> listObjectsV2(
@PathVariable String bucketName,
@RequestParam(required = false) String prefix,
@RequestParam(required = false) String delimiter,
@RequestParam(name = ENCODING_TYPE, required = false) String encodingType,
@RequestParam(name = START_AFTER, required = false) String startAfter,
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys,
@RequestParam(name = CONTINUATION_TOKEN, required = false) String continuationToken) {
bucketService.verifyBucketExists(bucketName);
bucketService.verifyMaxKeys(maxKeys);
bucketService.verifyEncodingType(encodingType);
var listBucketResultV2 =
bucketService.listObjectsV2(bucketName, prefix, delimiter, encodingType, startAfter,
maxKeys, continuationToken);
return ResponseEntity.ok(listBucketResultV2);
}
/**
* Retrieve list of versions of an object of a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectVersions.html">API Reference</a>
*
* @param bucketName {@link String} set bucket name
* @param prefix {@link String} find object names they start with prefix
* @param startAfter {@link String} return key names after a specific object key in your key
* space
* @param maxKeys {@link Integer} set maximum number of keys to be returned
* @param continuationToken {@link String} pagination token returned by previous request
*
* @return {@link ListVersionsResult} a list of objects in Bucket
*/
@GetMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
VERSIONS
},
produces = APPLICATION_XML_VALUE
)
public ResponseEntity<ListVersionsResult> listObjectVersions(
@PathVariable String bucketName,
@RequestParam(required = false) String prefix,
@RequestParam(required = false) String delimiter,
@RequestParam(name = KEY_MARKER, required = false) String keyMarker,
@RequestParam(name = VERSION_ID_MARKER, required = false) String versionIdMarker,
@RequestParam(name = ENCODING_TYPE, required = false) String encodingType,
@RequestParam(name = START_AFTER, required = false) String startAfter,
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys,
@RequestParam(name = CONTINUATION_TOKEN, required = false) String continuationToken) {
bucketService.verifyBucketExists(bucketName);
bucketService.verifyMaxKeys(maxKeys);
bucketService.verifyEncodingType(encodingType);
var listVersionsResult =
bucketService.listVersions(bucketName, prefix, delimiter, encodingType, startAfter,
maxKeys, continuationToken, keyMarker, versionIdMarker);
return ResponseEntity.ok(listVersionsResult);
}
}