File tree Expand file tree Collapse file tree 3 files changed +36
-8
lines changed
plugins/repository-gcs/src
main/java/org/elasticsearch/repositories/gcs
test/java/org/elasticsearch/repositories/gcs
test/framework/src/main/java/org/elasticsearch/repositories Expand file tree Collapse file tree 3 files changed +36
-8
lines changed Original file line number Diff line number Diff line change 5454import java .util .Map ;
5555import java .util .stream .Collectors ;
5656
57+ import static java .net .HttpURLConnection .HTTP_NOT_FOUND ;
5758import static java .net .HttpURLConnection .HTTP_PRECON_FAILED ;
5859
5960class GoogleCloudStorageBlobStore extends AbstractComponent implements BlobStore {
@@ -163,16 +164,19 @@ boolean blobExists(String blobName) throws IOException {
163164 */
164165 InputStream readBlob (String blobName ) throws IOException {
165166 final BlobId blobId = BlobId .of (bucketName , blobName );
166- final Blob blob = SocketAccess .doPrivilegedIOException (() -> client ().get (blobId ));
167- if (blob == null ) {
168- throw new NoSuchFileException ("Blob [" + blobName + "] does not exit" );
169- }
170- final ReadChannel readChannel = SocketAccess .doPrivilegedIOException (blob ::reader );
167+ final ReadChannel readChannel = SocketAccess .doPrivilegedIOException (() -> client ().reader (blobId ));
171168 return Channels .newInputStream (new ReadableByteChannel () {
172169 @ SuppressForbidden (reason = "Channel is based of a socket not a file" )
173170 @ Override
174171 public int read (ByteBuffer dst ) throws IOException {
175- return SocketAccess .doPrivilegedIOException (() -> readChannel .read (dst ));
172+ try {
173+ return SocketAccess .doPrivilegedIOException (() -> readChannel .read (dst ));
174+ } catch (StorageException e ) {
175+ if (e .getCode () == HTTP_NOT_FOUND ) {
176+ throw new NoSuchFileException ("Blob [" + blobName + "] does not exist" );
177+ }
178+ throw e ;
179+ }
176180 }
177181
178182 @ Override
Original file line number Diff line number Diff line change @@ -167,7 +167,27 @@ public Iterable<Blob> getValues() {
167167 public ReadChannel reader (BlobId blob , BlobSourceOption ... options ) {
168168 if (bucketName .equals (blob .getBucket ())) {
169169 final byte [] bytes = blobs .get (blob .getName ());
170- final ReadableByteChannel readableByteChannel = Channels .newChannel (new ByteArrayInputStream (bytes ));
170+
171+ final ReadableByteChannel readableByteChannel ;
172+ if (bytes != null ) {
173+ readableByteChannel = Channels .newChannel (new ByteArrayInputStream (bytes ));
174+ } else {
175+ readableByteChannel = new ReadableByteChannel () {
176+ @ Override
177+ public int read (ByteBuffer dst ) throws IOException {
178+ throw new StorageException (404 , "Object not found" );
179+ }
180+
181+ @ Override
182+ public boolean isOpen () {
183+ return false ;
184+ }
185+
186+ @ Override
187+ public void close () throws IOException {
188+ }
189+ };
190+ }
171191 return new ReadChannel () {
172192 @ Override
173193 public void close () {
Original file line number Diff line number Diff line change @@ -49,7 +49,11 @@ public abstract class ESBlobStoreContainerTestCase extends ESTestCase {
4949 public void testReadNonExistingPath () throws IOException {
5050 try (BlobStore store = newBlobStore ()) {
5151 final BlobContainer container = store .blobContainer (new BlobPath ());
52- expectThrows (NoSuchFileException .class , () -> container .readBlob ("non-existing" ));
52+ expectThrows (NoSuchFileException .class , () -> {
53+ try (InputStream is = container .readBlob ("non-existing" )) {
54+ is .read ();
55+ }
56+ });
5357 }
5458 }
5559
You can’t perform that action at this time.
0 commit comments