-
Notifications
You must be signed in to change notification settings - Fork 174
/
MultipartService.java
339 lines (318 loc) · 13.1 KB
/
MultipartService.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
/*
* 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.service;
import static com.adobe.testing.s3mock.S3Exception.ENTITY_TOO_SMALL;
import static com.adobe.testing.s3mock.S3Exception.INVALID_PART;
import static com.adobe.testing.s3mock.S3Exception.INVALID_PART_NUMBER;
import static com.adobe.testing.s3mock.S3Exception.INVALID_PART_ORDER;
import static com.adobe.testing.s3mock.S3Exception.NO_SUCH_UPLOAD_MULTIPART;
import com.adobe.testing.s3mock.S3Exception;
import com.adobe.testing.s3mock.dto.ChecksumAlgorithm;
import com.adobe.testing.s3mock.dto.CompleteMultipartUploadResult;
import com.adobe.testing.s3mock.dto.CompletedPart;
import com.adobe.testing.s3mock.dto.CopyPartResult;
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.Owner;
import com.adobe.testing.s3mock.dto.Part;
import com.adobe.testing.s3mock.dto.StorageClass;
import com.adobe.testing.s3mock.store.BucketStore;
import com.adobe.testing.s3mock.store.MultipartStore;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRange;
public class MultipartService extends ServiceBase {
private static final Logger LOG = LoggerFactory.getLogger(MultipartService.class);
static final Long MINIMUM_PART_SIZE = 5L * 1024L * 1024L;
private final BucketStore bucketStore;
private final MultipartStore multipartStore;
public MultipartService(BucketStore bucketStore, MultipartStore multipartStore) {
this.bucketStore = bucketStore;
this.multipartStore = multipartStore;
}
/**
* Uploads a part of a multipart upload.
*
* @param bucketName in which to upload
* @param key of the object to upload
* @param uploadId id of the upload
* @param partNumber number of the part to store
* @param path file data to be stored
*
* @return the md5 digest of this part
*/
public String putPart(String bucketName,
String key,
String uploadId,
String partNumber,
Path path,
Map<String, String> encryptionHeaders) {
var bucketMetadata = bucketStore.getBucketMetadata(bucketName);
var uuid = bucketMetadata.getID(key);
if (uuid == null) {
return null;
}
return multipartStore.putPart(bucketMetadata, uuid, uploadId, partNumber,
path, encryptionHeaders);
}
/**
* Copies the range, define by from/to, from the S3 Object, identified by the given key to given
* destination into the given bucket.
*
* @param bucketName The source Bucket.
* @param key Identifies the S3 Object.
* @param copyRange Byte range to copy. Optional.
* @param partNumber The part to copy.
* @param destinationBucket The Bucket the target object (will) reside in.
* @param destinationKey The target object key.
* @param uploadId id of the upload.
*
* @return etag of the uploaded file.
*/
public CopyPartResult copyPart(String bucketName,
String key,
HttpRange copyRange,
String partNumber,
String destinationBucket,
String destinationKey,
String uploadId,
Map<String, String> encryptionHeaders) {
var sourceBucketMetadata = bucketStore.getBucketMetadata(bucketName);
var destinationBucketMetadata = bucketStore.getBucketMetadata(destinationBucket);
var sourceId = sourceBucketMetadata.getID(key);
if (sourceId == null) {
return null;
}
// source must be copied to destination
var destinationId = bucketStore.addKeyToBucket(destinationKey, destinationBucket);
try {
var partEtag =
multipartStore.copyPart(sourceBucketMetadata, sourceId, copyRange, partNumber,
destinationBucketMetadata, destinationId, uploadId, encryptionHeaders);
return CopyPartResult.from(new Date(), "\"" + partEtag + "\"");
} catch (Exception e) {
//something went wrong with writing the destination file, clean up ID from BucketStore.
bucketStore.removeFromBucket(destinationKey, destinationBucket);
throw new IllegalStateException(String.format(
"Could not copy part. sourceBucket=%s, destinationBucket=%s, key=%s, sourceId=%s, "
+ "destinationId=%s, uploadId=%s", sourceBucketMetadata, destinationBucketMetadata,
key, sourceId, destinationId, uploadId
), e);
}
}
/**
* Get all multipart upload parts.
* @param bucketName name of the bucket
* @param key object key
* @param uploadId upload identifier
* @return List of Parts
*/
public ListPartsResult getMultipartUploadParts(String bucketName, String key, String uploadId) {
var bucketMetadata = bucketStore.getBucketMetadata(bucketName);
var id = bucketMetadata.getID(key);
if (id == null) {
return null;
}
var parts = multipartStore.getMultipartUploadParts(bucketMetadata, id, uploadId);
return new ListPartsResult(bucketName, key, uploadId, parts);
}
/**
* Aborts the upload.
*
* @param bucketName to which was uploaded
* @param key which was uploaded
* @param uploadId of the upload
*/
public void abortMultipartUpload(String bucketName, String key, String uploadId) {
var bucketMetadata = bucketStore.getBucketMetadata(bucketName);
var id = bucketMetadata.getID(key);
try {
multipartStore.abortMultipartUpload(bucketMetadata, id, uploadId);
} finally {
bucketStore.removeFromBucket(key, bucketName);
}
}
/**
* Completes a Multipart Upload for the given ID.
*
* @param bucketName in which to upload.
* @param key of the file to upload.
* @param uploadId id of the upload.
* @param parts to concatenate.
* @param location the location link to embed in result
*
* @return etag of the uploaded file.
*/
public CompleteMultipartUploadResult completeMultipartUpload(
String bucketName,
String key,
String uploadId,
List<CompletedPart> parts,
Map<String, String> encryptionHeaders,
String location) {
var bucketMetadata = bucketStore.getBucketMetadata(bucketName);
var id = bucketMetadata.getID(key);
if (id == null) {
return null;
}
var multipartUploadInfo = multipartStore.getMultipartUploadInfo(bucketMetadata, uploadId);
return multipartStore
.completeMultipartUpload(bucketMetadata, key, id, uploadId, parts, encryptionHeaders,
multipartUploadInfo, location);
}
/**
* Prepares everything to store an object uploaded as multipart upload.
*
* @param bucketName Bucket to upload object in
* @param key object to upload
* @param contentType the content type
* @param storeHeaders various headers to store
* @param owner owner of the upload
* @param initiator initiator of the upload
* @param userMetadata custom metadata
*
* @return upload result
*/
public InitiateMultipartUploadResult createMultipartUpload(String bucketName,
String key,
String contentType,
Map<String, String> storeHeaders,
Owner owner,
Owner initiator,
Map<String, String> userMetadata,
Map<String, String> encryptionHeaders,
StorageClass storageClass,
String checksum,
ChecksumAlgorithm checksumAlgorithm) {
var bucketMetadata = bucketStore.getBucketMetadata(bucketName);
var id = bucketStore.addKeyToBucket(key, bucketName);
try {
var multipartUpload = multipartStore.createMultipartUpload(bucketMetadata,
key,
id,
contentType,
storeHeaders,
owner,
initiator,
userMetadata,
encryptionHeaders,
storageClass,
checksum,
checksumAlgorithm);
return new InitiateMultipartUploadResult(bucketName, key, multipartUpload.uploadId());
} catch (Exception e) {
//something went wrong with writing the destination file, clean up ID from BucketStore.
bucketStore.removeFromBucket(key, bucketName);
throw new IllegalStateException(String.format(
"Could prepare Multipart Upload. bucket=%s, key=%s, id=%s",
bucketMetadata, key, id
), e);
}
}
/**
* Lists all not-yet completed parts of multipart uploads in a bucket.
*
* @param bucketName the bucket to use as a filter
* @param prefix the prefix use as a filter
*
* @return the list of not-yet completed multipart uploads.
*/
public ListMultipartUploadsResult listMultipartUploads(String bucketName, String prefix) {
var bucketMetadata = bucketStore.getBucketMetadata(bucketName);
var multipartUploads =
multipartStore.listMultipartUploads(bucketMetadata, prefix);
// the result contains all uploads, use some common value as default
var maxUploads = Math.max(1000, multipartUploads.size());
// delimiter / prefix search not supported
return new ListMultipartUploadsResult(bucketName, null, null, prefix, null,
maxUploads, false, null, null, multipartUploads,
Collections.emptyList());
}
public void verifyPartNumberLimits(String partNumberString) {
int partNumber;
try {
partNumber = Integer.parseInt(partNumberString);
if (partNumber < 1 || partNumber > 10000) {
LOG.error("Multipart part number invalid. partNumber={}", partNumberString);
throw INVALID_PART_NUMBER;
}
} catch (NumberFormatException nfe) {
LOG.error("Multipart part number invalid. partNumber={}", partNumberString, nfe);
throw INVALID_PART_NUMBER;
}
}
public void verifyMultipartParts(String bucketName, String key,
String uploadId, List<CompletedPart> requestedParts) throws S3Exception {
var bucketMetadata = bucketStore.getBucketMetadata(bucketName);
var id = bucketMetadata.getID(key);
if (id == null) {
throw INVALID_PART;
}
verifyMultipartParts(bucketName, id, uploadId);
var uploadedParts = multipartStore.getMultipartUploadParts(bucketMetadata, id, uploadId);
var uploadedPartsMap =
uploadedParts
.stream()
.collect(Collectors.toMap(Part::partNumber, Part::etag));
var prevPartNumber = 0;
for (var part : requestedParts) {
if (!uploadedPartsMap.containsKey(part.partNumber())
|| !uploadedPartsMap.get(part.partNumber()).equals(part.etag())) {
LOG.error("Multipart part not valid. bucket={}, id={}, uploadId={}, partNumber={}",
bucketMetadata, id, uploadId, part.partNumber());
throw INVALID_PART;
}
if (part.partNumber() < prevPartNumber) {
LOG.error("Multipart parts order invalid. bucket={}, id={}, uploadId={}, partNumber={}",
bucketMetadata, id, uploadId, part.partNumber());
throw INVALID_PART_ORDER;
}
prevPartNumber = part.partNumber();
}
}
public void verifyMultipartParts(String bucketName, UUID id, String uploadId) throws S3Exception {
verifyMultipartUploadExists(bucketName, uploadId);
var bucketMetadata = bucketStore.getBucketMetadata(bucketName);
var uploadedParts = multipartStore.getMultipartUploadParts(bucketMetadata, id, uploadId);
if (!uploadedParts.isEmpty()) {
for (int i = 0; i < uploadedParts.size() - 1; i++) {
var part = uploadedParts.get(i);
if (part.size() < MINIMUM_PART_SIZE) {
LOG.error("Multipart part size too small. bucket={}, id={}, uploadId={}, size={}",
bucketMetadata, id, uploadId, part.size());
throw ENTITY_TOO_SMALL;
}
}
}
}
public void verifyMultipartUploadExists(String bucketName,
String uploadId) throws S3Exception {
try {
var bucketMetadata = bucketStore.getBucketMetadata(bucketName);
multipartStore.getMultipartUpload(bucketMetadata, uploadId);
} catch (IllegalArgumentException e) {
throw NO_SUCH_UPLOAD_MULTIPART;
}
}
}