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
3 files changed +36
-8
lines changed Original file line number Diff line number Diff line change 54
54
import java .util .Map ;
55
55
import java .util .stream .Collectors ;
56
56
57
+ import static java .net .HttpURLConnection .HTTP_NOT_FOUND ;
57
58
import static java .net .HttpURLConnection .HTTP_PRECON_FAILED ;
58
59
59
60
class GoogleCloudStorageBlobStore extends AbstractComponent implements BlobStore {
@@ -163,16 +164,19 @@ boolean blobExists(String blobName) throws IOException {
163
164
*/
164
165
InputStream readBlob (String blobName ) throws IOException {
165
166
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 ));
171
168
return Channels .newInputStream (new ReadableByteChannel () {
172
169
@ SuppressForbidden (reason = "Channel is based of a socket not a file" )
173
170
@ Override
174
171
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
+ }
176
180
}
177
181
178
182
@ Override
Original file line number Diff line number Diff line change @@ -167,7 +167,27 @@ public Iterable<Blob> getValues() {
167
167
public ReadChannel reader (BlobId blob , BlobSourceOption ... options ) {
168
168
if (bucketName .equals (blob .getBucket ())) {
169
169
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
+ }
171
191
return new ReadChannel () {
172
192
@ Override
173
193
public void close () {
Original file line number Diff line number Diff line change @@ -49,7 +49,11 @@ public abstract class ESBlobStoreContainerTestCase extends ESTestCase {
49
49
public void testReadNonExistingPath () throws IOException {
50
50
try (BlobStore store = newBlobStore ()) {
51
51
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
+ });
53
57
}
54
58
}
55
59
You can’t perform that action at this time.
0 commit comments