Skip to content

Commit f3e1e21

Browse files
committed
S3 copyBlob: convert amazon exception to NoSuchFileException when source is not found
1 parent 8313592 commit f3e1e21

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobContainer.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@
6565
import org.elasticsearch.repositories.RepositoryException;
6666
import org.elasticsearch.repositories.blobstore.ChunkedBlobOutputStream;
6767
import org.elasticsearch.repositories.s3.S3BlobStore.Operation;
68+
import org.elasticsearch.rest.RestStatus;
6869
import org.elasticsearch.threadpool.ThreadPool;
6970

7071
import java.io.ByteArrayInputStream;
7172
import java.io.IOException;
7273
import java.io.InputStream;
7374
import java.io.OutputStream;
75+
import java.nio.file.NoSuchFileException;
7476
import java.time.Instant;
7577
import java.util.ArrayList;
7678
import java.util.Date;
@@ -350,27 +352,35 @@ public void copyBlob(
350352

351353
final var s3SourceBlobContainer = (S3BlobContainer) sourceBlobContainer;
352354

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());
363367

364-
S3BlobStore.configureRequestForMetrics(copyRequest, blobStore, Operation.COPY_OBJECT, purpose);
368+
S3BlobStore.configureRequestForMetrics(copyRequest, blobStore, Operation.COPY_OBJECT, purpose);
365369

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+
}
373382
}
383+
throw new IOException("Unable to copy object [" + blobName + "] from [" + sourceBlobContainer + "][" + sourceBlobName + "]", e);
374384
}
375385
}
376386

0 commit comments

Comments
 (0)