/** Example of checking if the blob exists. */ // [TARGET exists(BlobSourceOption...)] public boolean exists() { // [START exists] boolean exists = blob.exists(); if (exists) { // the blob exists } else { // the blob was not found } // [END exists] return exists; }
@Test public void testExists_False() throws Exception { Storage.BlobGetOption[] expectedOptions = {Storage.BlobGetOption.fields()}; expect(storage.getOptions()).andReturn(null); expect(storage.get(BLOB_INFO.getBlobId(), expectedOptions)).andReturn(null); replay(storage); initializeBlob(); assertFalse(blob.exists()); }
@Test public void testExists_True() throws Exception { initializeExpectedBlob(1); Storage.BlobGetOption[] expectedOptions = {Storage.BlobGetOption.fields()}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(expectedBlob.getBlobId(), expectedOptions)).andReturn(expectedBlob); replay(storage); initializeBlob(); assertTrue(blob.exists()); }
/** * Returns the output stream for a Google Cloud Storage file. * @return the object's output stream or {@code null} if the object doesn't exist and cannot be * created * @throws IOException if an issue occurs getting the OutputStream */ @Override public OutputStream getOutputStream() throws IOException { if (isBucket()) { throw new IllegalStateException("Cannot open an output stream to a bucket: '" + this.location + "'"); } else { Blob blob = getBlob(); if (blob == null || !blob.exists()) { if (!this.autoCreateFiles) { throw new FileNotFoundException("The blob was not found: " + this.location); } blob = createBlob(); } return Channels.newOutputStream(blob.writer()); } }
/** * Returns the output stream for a Google Cloud Storage file. * @return the object's output stream or {@code null} if the object doesn't exist and cannot be * created * @throws IOException if an issue occurs getting the OutputStream */ @Override public OutputStream getOutputStream() throws IOException { if (isBucket()) { throw new IllegalStateException("Cannot open an output stream to a bucket: '" + this.location + "'"); } else { Blob blob = getBlob(); if (blob == null || !blob.exists()) { if (!this.autoCreateFiles) { throw new FileNotFoundException("The blob was not found: " + this.location); } blob = createBlob(); } return Channels.newOutputStream(blob.writer()); } }
@Override public boolean exists(String path) { final String bk[] = split(path); if (bk.length == 1) { return storageProvider.getStorage().get(bk[0]).exists(); } else if (bk.length == 2) { Blob blob = storageProvider.getStorage().get(bk[0], bk[1]); return blob != null && blob.exists(); } else { return false; } }