|
65 | 65 | import org.elasticsearch.repositories.RepositoryException; |
66 | 66 | import org.elasticsearch.repositories.blobstore.ChunkedBlobOutputStream; |
67 | 67 | import org.elasticsearch.repositories.s3.S3BlobStore.Operation; |
| 68 | +import org.elasticsearch.rest.RestStatus; |
68 | 69 | import org.elasticsearch.threadpool.ThreadPool; |
69 | 70 |
|
70 | 71 | import java.io.ByteArrayInputStream; |
71 | 72 | import java.io.IOException; |
72 | 73 | import java.io.InputStream; |
73 | 74 | import java.io.OutputStream; |
| 75 | +import java.nio.file.NoSuchFileException; |
74 | 76 | import java.time.Instant; |
75 | 77 | import java.util.ArrayList; |
76 | 78 | import java.util.Date; |
@@ -350,27 +352,35 @@ public void copyBlob( |
350 | 352 |
|
351 | 353 | final var s3SourceBlobContainer = (S3BlobContainer) sourceBlobContainer; |
352 | 354 |
|
353 | | - if (blobSize > MAX_FILE_SIZE.getBytes()) { |
354 | | - executeMultipartCopy(purpose, s3SourceBlobContainer, sourceBlobName, blobName, blobSize); |
355 | | - } else { |
356 | | - // metadata is inherited from source, but not canned ACL or storage class |
357 | | - final CopyObjectRequest copyRequest = new CopyObjectRequest( |
358 | | - s3SourceBlobContainer.blobStore.bucket(), |
359 | | - s3SourceBlobContainer.buildKey(sourceBlobName), |
360 | | - blobStore.bucket(), |
361 | | - buildKey(blobName) |
362 | | - ).withCannedAccessControlList(blobStore.getCannedACL()).withStorageClass(blobStore.getStorageClass()); |
| 355 | + try { |
| 356 | + if (blobSize > MAX_FILE_SIZE.getBytes()) { |
| 357 | + executeMultipartCopy(purpose, s3SourceBlobContainer, sourceBlobName, blobName, blobSize); |
| 358 | + } else { |
| 359 | + // metadata is inherited from source, but not canned ACL or storage class |
| 360 | + final var blobKey = buildKey(blobName); |
| 361 | + final CopyObjectRequest copyRequest = new CopyObjectRequest( |
| 362 | + s3SourceBlobContainer.blobStore.bucket(), |
| 363 | + s3SourceBlobContainer.buildKey(sourceBlobName), |
| 364 | + blobStore.bucket(), |
| 365 | + blobKey |
| 366 | + ).withCannedAccessControlList(blobStore.getCannedACL()).withStorageClass(blobStore.getStorageClass()); |
363 | 367 |
|
364 | | - S3BlobStore.configureRequestForMetrics(copyRequest, blobStore, Operation.COPY_OBJECT, purpose); |
| 368 | + S3BlobStore.configureRequestForMetrics(copyRequest, blobStore, Operation.COPY_OBJECT, purpose); |
365 | 369 |
|
366 | | - try (AmazonS3Reference clientReference = blobStore.clientReference()) { |
367 | | - SocketAccess.doPrivilegedVoid(() -> { clientReference.client().copyObject(copyRequest); }); |
368 | | - } catch (final AmazonClientException e) { |
369 | | - throw new IOException( |
370 | | - "Unable to copy object [" + blobName + "] from [" + sourceBlobContainer + "][" + sourceBlobName + "]", |
371 | | - e |
372 | | - ); |
| 370 | + try (AmazonS3Reference clientReference = blobStore.clientReference()) { |
| 371 | + SocketAccess.doPrivilegedVoid(() -> { |
| 372 | + clientReference.client().copyObject(copyRequest); |
| 373 | + }); |
| 374 | + } |
| 375 | + } |
| 376 | + } catch (final AmazonClientException e) { |
| 377 | + if (e instanceof AmazonS3Exception amazonS3Exception) { |
| 378 | + if (amazonS3Exception.getStatusCode() == RestStatus.NOT_FOUND.getStatus()) { |
| 379 | + final var sourceKey = s3SourceBlobContainer.buildKey(sourceBlobName); |
| 380 | + throw new NoSuchFileException("Copy source [" + sourceKey + "] not found: " + amazonS3Exception.getMessage()); |
| 381 | + } |
373 | 382 | } |
| 383 | + throw new IOException("Unable to copy object [" + blobName + "] from [" + sourceBlobContainer + "][" + sourceBlobName + "]", e); |
374 | 384 | } |
375 | 385 | } |
376 | 386 |
|
|
0 commit comments