diff --git a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java index c20b99790088e..2bfb3afc7d36c 100644 --- a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java +++ b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java @@ -54,6 +54,7 @@ import java.util.Map; import java.util.stream.Collectors; +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; import static java.net.HttpURLConnection.HTTP_PRECON_FAILED; class GoogleCloudStorageBlobStore extends AbstractComponent implements BlobStore { @@ -163,16 +164,19 @@ boolean blobExists(String blobName) throws IOException { */ InputStream readBlob(String blobName) throws IOException { final BlobId blobId = BlobId.of(bucketName, blobName); - final Blob blob = SocketAccess.doPrivilegedIOException(() -> client().get(blobId)); - if (blob == null) { - throw new NoSuchFileException("Blob [" + blobName + "] does not exit"); - } - final ReadChannel readChannel = SocketAccess.doPrivilegedIOException(blob::reader); + final ReadChannel readChannel = SocketAccess.doPrivilegedIOException(() -> client().reader(blobId)); return Channels.newInputStream(new ReadableByteChannel() { @SuppressForbidden(reason = "Channel is based of a socket not a file") @Override public int read(ByteBuffer dst) throws IOException { - return SocketAccess.doPrivilegedIOException(() -> readChannel.read(dst)); + try { + return SocketAccess.doPrivilegedIOException(() -> readChannel.read(dst)); + } catch (StorageException e) { + if (e.getCode() == HTTP_NOT_FOUND) { + throw new NoSuchFileException("Blob [" + blobName + "] does not exist"); + } + throw e; + } } @Override diff --git a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/MockStorage.java b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/MockStorage.java index 605d1798ee826..cf7395ea1f1f7 100644 --- a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/MockStorage.java +++ b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/MockStorage.java @@ -167,7 +167,27 @@ public Iterable getValues() { public ReadChannel reader(BlobId blob, BlobSourceOption... options) { if (bucketName.equals(blob.getBucket())) { final byte[] bytes = blobs.get(blob.getName()); - final ReadableByteChannel readableByteChannel = Channels.newChannel(new ByteArrayInputStream(bytes)); + + final ReadableByteChannel readableByteChannel; + if (bytes != null) { + readableByteChannel = Channels.newChannel(new ByteArrayInputStream(bytes)); + } else { + readableByteChannel = new ReadableByteChannel() { + @Override + public int read(ByteBuffer dst) throws IOException { + throw new StorageException(404, "Object not found"); + } + + @Override + public boolean isOpen() { + return false; + } + + @Override + public void close() throws IOException { + } + }; + } return new ReadChannel() { @Override public void close() { diff --git a/test/framework/src/main/java/org/elasticsearch/repositories/ESBlobStoreContainerTestCase.java b/test/framework/src/main/java/org/elasticsearch/repositories/ESBlobStoreContainerTestCase.java index 13f9e9debc966..43a62bbe662cc 100644 --- a/test/framework/src/main/java/org/elasticsearch/repositories/ESBlobStoreContainerTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/repositories/ESBlobStoreContainerTestCase.java @@ -49,7 +49,11 @@ public abstract class ESBlobStoreContainerTestCase extends ESTestCase { public void testReadNonExistingPath() throws IOException { try(BlobStore store = newBlobStore()) { final BlobContainer container = store.blobContainer(new BlobPath()); - expectThrows(NoSuchFileException.class, () -> container.readBlob("non-existing")); + expectThrows(NoSuchFileException.class, () -> { + try (InputStream is = container.readBlob("non-existing")) { + is.read(); + } + }); } }