-
Notifications
You must be signed in to change notification settings - Fork 174
/
MultipartController.java
398 lines (367 loc) · 15.2 KB
/
MultipartController.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
/*
* 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.dto.Owner.DEFAULT_OWNER;
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.NOT_X_AMZ_COPY_SOURCE;
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.NOT_X_AMZ_COPY_SOURCE_RANGE;
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.X_AMZ_COPY_SOURCE;
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.X_AMZ_COPY_SOURCE_IF_MATCH;
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.X_AMZ_COPY_SOURCE_IF_NONE_MATCH;
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.X_AMZ_COPY_SOURCE_RANGE;
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.X_AMZ_STORAGE_CLASS;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.NOT_LIFECYCLE;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.PART_NUMBER;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.UPLOADS;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.UPLOAD_ID;
import static com.adobe.testing.s3mock.util.HeaderUtil.checksumAlgorithmFromHeader;
import static com.adobe.testing.s3mock.util.HeaderUtil.checksumAlgorithmFromSdk;
import static com.adobe.testing.s3mock.util.HeaderUtil.checksumFrom;
import static com.adobe.testing.s3mock.util.HeaderUtil.checksumHeaderFrom;
import static com.adobe.testing.s3mock.util.HeaderUtil.encryptionHeadersFrom;
import static com.adobe.testing.s3mock.util.HeaderUtil.storeHeadersFrom;
import static com.adobe.testing.s3mock.util.HeaderUtil.userMetadataFrom;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
import com.adobe.testing.s3mock.dto.ChecksumAlgorithm;
import com.adobe.testing.s3mock.dto.CompleteMultipartUpload;
import com.adobe.testing.s3mock.dto.CompleteMultipartUploadResult;
import com.adobe.testing.s3mock.dto.CopyPartResult;
import com.adobe.testing.s3mock.dto.CopySource;
import com.adobe.testing.s3mock.dto.InitiateMultipartUploadResult;
import com.adobe.testing.s3mock.dto.ListMultipartUploadsResult;
import com.adobe.testing.s3mock.dto.ListPartsResult;
import com.adobe.testing.s3mock.dto.ObjectKey;
import com.adobe.testing.s3mock.dto.StorageClass;
import com.adobe.testing.s3mock.service.BucketService;
import com.adobe.testing.s3mock.service.MultipartService;
import com.adobe.testing.s3mock.service.ObjectService;
import jakarta.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRange;
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.PostMapping;
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.RequestParam;
import software.amazon.awssdk.utils.http.SdkHttpUtils;
/**
* Handles requests related to parts.
*/
@CrossOrigin(origins = "*", exposedHeaders = "*")
@Controller
@RequestMapping("${com.adobe.testing.s3mock.contextPath:}")
public class MultipartController {
private final BucketService bucketService;
private final ObjectService objectService;
private final MultipartService multipartService;
public MultipartController(BucketService bucketService, ObjectService objectService,
MultipartService multipartService) {
this.bucketService = bucketService;
this.objectService = objectService;
this.multipartService = multipartService;
}
//================================================================================================
// /{bucketName:.+}
//================================================================================================
/**
* Lists all in-progress multipart uploads.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListMultipartUploads.html">API Reference</a>
*
* <p>Not yet supported request parameters: delimiter, encoding-type, max-uploads, key-marker,
* upload-id-marker.</p>
*
* @param bucketName the Bucket in which to store the file in.
*
* @return the {@link ListMultipartUploadsResult}
*/
@GetMapping(
value = {
//AWS SDK V2 pattern
"/{bucketName:.+}",
//AWS SDK V1 pattern
"/{bucketName:.+}/"
},
params = {
UPLOADS
},
produces = APPLICATION_XML_VALUE
)
public ResponseEntity<ListMultipartUploadsResult> listMultipartUploads(
@PathVariable String bucketName,
@RequestParam(required = false) String prefix) {
bucketService.verifyBucketExists(bucketName);
return ResponseEntity.ok(multipartService.listMultipartUploads(bucketName, prefix));
}
//================================================================================================
// /{bucketName:.+}/{*key}
//================================================================================================
/**
* Aborts a multipart upload for a given uploadId.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_AbortMultipartUpload.html">API Reference</a>
*
* @param bucketName the Bucket in which to store the file in.
* @param uploadId id of the upload. Has to match all other part's uploads.
*/
@DeleteMapping(
value = "/{bucketName:.+}/{*key}",
params = {
UPLOAD_ID,
NOT_LIFECYCLE
},
produces = APPLICATION_XML_VALUE
)
public ResponseEntity<Void> abortMultipartUpload(@PathVariable String bucketName,
@PathVariable ObjectKey key,
@RequestParam String uploadId) {
bucketService.verifyBucketExists(bucketName);
multipartService.verifyMultipartUploadExists(bucketName, uploadId);
multipartService.abortMultipartUpload(bucketName, key.key(), uploadId);
return ResponseEntity.noContent().build();
}
/**
* Lists all parts a file multipart upload.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListParts.html">API Reference</a>
*
* @param bucketName the Bucket in which to store the file in.
* @param uploadId id of the upload. Has to match all other part's uploads.
*
* @return the {@link ListPartsResult}
*/
@GetMapping(
value = "/{bucketName:.+}/{*key}",
params = {
UPLOAD_ID
},
produces = APPLICATION_XML_VALUE
)
public ResponseEntity<ListPartsResult> listParts(@PathVariable String bucketName,
@PathVariable ObjectKey key,
@RequestParam String uploadId) {
bucketService.verifyBucketExists(bucketName);
multipartService.verifyMultipartUploadExists(bucketName, uploadId);
return ResponseEntity
.ok(multipartService.getMultipartUploadParts(bucketName, key.key(), uploadId));
}
/**
* Adds an object to a bucket accepting encryption headers.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPart.html">API Reference</a>
*
* @param bucketName the Bucket in which to store the file in.
* @param uploadId id of the upload. Has to match all other part's uploads.
* @param partNumber number of the part to upload.
*
* @return the etag of the uploaded part.
*
*/
@PutMapping(
value = "/{bucketName:.+}/{*key}",
params = {
UPLOAD_ID,
PART_NUMBER
},
headers = {
NOT_X_AMZ_COPY_SOURCE,
NOT_X_AMZ_COPY_SOURCE_RANGE
}
)
public ResponseEntity<Void> uploadPart(@PathVariable String bucketName,
@PathVariable ObjectKey key,
@RequestParam String uploadId,
@RequestParam String partNumber,
@RequestHeader HttpHeaders httpHeaders,
InputStream inputStream) {
final var tempFileAndChecksum = multipartService.toTempFile(inputStream, httpHeaders);
bucketService.verifyBucketExists(bucketName);
multipartService.verifyMultipartUploadExists(bucketName, uploadId);
multipartService.verifyPartNumberLimits(partNumber);
String checksum = null;
ChecksumAlgorithm checksumAlgorithm = null;
ChecksumAlgorithm algorithmFromSdk = checksumAlgorithmFromSdk(httpHeaders);
if (algorithmFromSdk != null) {
checksum = tempFileAndChecksum.getRight();
checksumAlgorithm = algorithmFromSdk;
}
ChecksumAlgorithm algorithmFromHeader = checksumAlgorithmFromHeader(httpHeaders);
if (algorithmFromHeader != null) {
checksum = checksumFrom(httpHeaders);
checksumAlgorithm = algorithmFromHeader;
}
var tempFile = tempFileAndChecksum.getLeft();
if (checksum != null) {
multipartService.verifyChecksum(tempFile, checksum, checksumAlgorithm);
}
//persist checksum per part
var etag = multipartService.putPart(bucketName,
key.key(),
uploadId,
partNumber,
tempFile,
encryptionHeadersFrom(httpHeaders));
FileUtils.deleteQuietly(tempFile.toFile());
Map<String, String> checksumHeader = checksumHeaderFrom(checksum, checksumAlgorithm);
return ResponseEntity
.ok()
.headers(h -> h.setAll(checksumHeader))
.headers(h -> h.setAll(encryptionHeadersFrom(httpHeaders)))
.eTag("\"" + etag + "\"")
.build();
}
/**
* Uploads a part by copying data from an existing object as data source.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPartCopy.html">API Reference</a>
*
* @param copySource References the Objects to be copied.
* @param copyRange Defines the byte range for this part. Optional.
* @param uploadId id of the upload. Has to match all other part's uploads.
* @param partNumber number of the part to upload.
*
* @return The etag of the uploaded part.
*
*/
@PutMapping(
value = "/{bucketName:.+}/{*key}",
headers = {
X_AMZ_COPY_SOURCE,
},
params = {
UPLOAD_ID,
PART_NUMBER
},
produces = APPLICATION_XML_VALUE)
public ResponseEntity<CopyPartResult> uploadPartCopy(
@PathVariable String bucketName,
@PathVariable ObjectKey key,
@RequestHeader(value = X_AMZ_COPY_SOURCE) CopySource copySource,
@RequestHeader(value = X_AMZ_COPY_SOURCE_RANGE, required = false) HttpRange copyRange,
@RequestHeader(value = X_AMZ_COPY_SOURCE_IF_MATCH, required = false) List<String> match,
@RequestHeader(value = X_AMZ_COPY_SOURCE_IF_NONE_MATCH,
required = false) List<String> noneMatch,
@RequestParam String uploadId,
@RequestParam String partNumber,
@RequestHeader HttpHeaders httpHeaders) {
//needs modified-since handling, see API
bucketService.verifyBucketExists(bucketName);
multipartService.verifyPartNumberLimits(partNumber);
var s3ObjectMetadata = objectService.verifyObjectExists(copySource.bucket(), copySource.key());
objectService.verifyObjectMatchingForCopy(match, noneMatch, s3ObjectMetadata);
var result = multipartService.copyPart(copySource.bucket(),
copySource.key(),
copyRange,
partNumber,
bucketName,
key.key(),
uploadId,
encryptionHeadersFrom(httpHeaders)
);
//return encryption headers
//return source version id
return ResponseEntity.ok(result);
}
/**
* Initiates a multipart upload accepting encryption headers.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html">API Reference</a>
*
* @param bucketName the Bucket in which to store the file in.
*
* @return the {@link InitiateMultipartUploadResult}.
*/
@PostMapping(
value = "/{bucketName:.+}/{*key}",
params = {
UPLOADS
},
produces = APPLICATION_XML_VALUE)
public ResponseEntity<InitiateMultipartUploadResult> createMultipartUpload(
@PathVariable String bucketName,
@PathVariable ObjectKey key,
@RequestHeader(value = CONTENT_TYPE, required = false) String contentType,
@RequestHeader(value = X_AMZ_STORAGE_CLASS, required = false, defaultValue = "STANDARD")
StorageClass storageClass,
@RequestHeader HttpHeaders httpHeaders) {
bucketService.verifyBucketExists(bucketName);
var result =
multipartService.createMultipartUpload(bucketName,
key.key(),
contentType,
storeHeadersFrom(httpHeaders),
DEFAULT_OWNER,
DEFAULT_OWNER,
userMetadataFrom(httpHeaders),
encryptionHeadersFrom(httpHeaders),
storageClass,
checksumFrom(httpHeaders),
checksumAlgorithmFromHeader(httpHeaders));
//return encryption headers
//return checksum algorithm headers
return ResponseEntity.ok(result);
}
/**
* Adds an object to a bucket.
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html">API Reference</a>
*
* @param bucketName the Bucket in which to store the file in.
* @param uploadId id of the upload. Has to match all other part's uploads.
*
* @return {@link CompleteMultipartUploadResult}
*/
@PostMapping(
value = "/{bucketName:.+}/{*key}",
params = {
UPLOAD_ID
},
produces = APPLICATION_XML_VALUE)
public ResponseEntity<CompleteMultipartUploadResult> completeMultipartUpload(
@PathVariable String bucketName,
@PathVariable ObjectKey key,
@RequestParam String uploadId,
@RequestBody CompleteMultipartUpload upload,
HttpServletRequest request,
@RequestHeader HttpHeaders httpHeaders) {
bucketService.verifyBucketExists(bucketName);
multipartService.verifyMultipartUploadExists(bucketName, uploadId);
multipartService.verifyMultipartParts(bucketName, key.key(), uploadId, upload.parts());
var objectName = key.key();
var locationWithEncodedKey = request
.getRequestURL()
.toString()
.replace(objectName, SdkHttpUtils.urlEncode(objectName));
var result = multipartService.completeMultipartUpload(bucketName,
key.key(),
uploadId,
upload.parts(),
encryptionHeadersFrom(httpHeaders),
locationWithEncodedKey);
String checksum = result.checksum();
ChecksumAlgorithm checksumAlgorithm = result.multipartUploadInfo().checksumAlgorithm();
//return encryption headers.
//return version id
return ResponseEntity
.ok()
.headers(h -> h.setAll(checksumHeaderFrom(checksum, checksumAlgorithm)))
.body(result);
}
}