use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadChannelFailUpdatedGeneration.
@Test
public void testReadChannelFailUpdatedGeneration() throws IOException {
String blobName = "test-read-blob-fail-updated-generation";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Random random = new Random();
int chunkSize = 1024;
int blobSize = 2 * chunkSize;
byte[] content = new byte[blobSize];
random.nextBytes(content);
Blob remoteBlob = storage.create(blob, content);
assertNotNull(remoteBlob);
assertEquals(blobSize, (long) remoteBlob.getSize());
try (ReadChannel reader = storage.reader(blob.getBlobId())) {
reader.setChunkSize(chunkSize);
ByteBuffer readBytes = ByteBuffer.allocate(chunkSize);
int numReadBytes = reader.read(readBytes);
assertEquals(chunkSize, numReadBytes);
assertArrayEquals(Arrays.copyOf(content, chunkSize), readBytes.array());
try (WriteChannel writer = storage.writer(blob)) {
byte[] newContent = new byte[blobSize];
random.nextBytes(newContent);
int numWrittenBytes = writer.write(ByteBuffer.wrap(newContent));
assertEquals(blobSize, numWrittenBytes);
}
readBytes = ByteBuffer.allocate(chunkSize);
reader.read(readBytes);
fail("StorageException was expected");
} catch (StorageException ex) {
StringBuilder messageBuilder = new StringBuilder();
messageBuilder.append("Blob ").append(blob.getBlobId()).append(" was updated while reading");
assertEquals(messageBuilder.toString(), ex.getMessage());
}
assertTrue(storage.delete(BUCKET, blobName));
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadChannelFail.
@Test
public void testReadChannelFail() throws IOException {
String blobName = "test-read-channel-blob-fail";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Blob remoteBlob = storage.create(blob);
assertNotNull(remoteBlob);
try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.metagenerationMatch(-1L))) {
reader.read(ByteBuffer.allocate(42));
fail("StorageException was expected");
} catch (StorageException ex) {
// expected
}
try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.generationMatch(-1L))) {
reader.read(ByteBuffer.allocate(42));
fail("StorageException was expected");
} catch (StorageException ex) {
// expected
}
BlobId blobIdWrongGeneration = BlobId.of(BUCKET, blobName, -1L);
try (ReadChannel reader = storage.reader(blobIdWrongGeneration, Storage.BlobSourceOption.generationMatch())) {
reader.read(ByteBuffer.allocate(42));
fail("StorageException was expected");
} catch (StorageException ex) {
// expected
}
assertTrue(remoteBlob.delete());
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testGetBlobFail.
@Test
public void testGetBlobFail() {
String blobName = "test-get-blob-fail";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Blob remoteBlob = storage.create(blob);
assertNotNull(remoteBlob);
BlobId wrongGenerationBlob = BlobId.of(BUCKET, blobName);
try {
storage.get(wrongGenerationBlob, Storage.BlobGetOption.generationMatch(-1));
fail("StorageException was expected");
} catch (StorageException ex) {
// expected
}
assertTrue(remoteBlob.delete());
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testListBlobsSelectedFields.
@Test(timeout = 5000)
public void testListBlobsSelectedFields() throws InterruptedException {
String[] blobNames = { "test-list-blobs-selected-fields-blob1", "test-list-blobs-selected-fields-blob2" };
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
BlobInfo blob1 = BlobInfo.newBuilder(BUCKET, blobNames[0]).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
BlobInfo blob2 = BlobInfo.newBuilder(BUCKET, blobNames[1]).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
Blob remoteBlob1 = storage.create(blob1);
Blob remoteBlob2 = storage.create(blob2);
assertNotNull(remoteBlob1);
assertNotNull(remoteBlob2);
Page<Blob> page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-selected-fields-blob"), Storage.BlobListOption.fields(BlobField.METADATA));
// test fails if timeout is reached.
while (Iterators.size(page.iterateAll().iterator()) != 2) {
Thread.sleep(500);
page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-selected-fields-blob"), Storage.BlobListOption.fields(BlobField.METADATA));
}
Set<String> blobSet = ImmutableSet.of(blobNames[0], blobNames[1]);
Iterator<Blob> iterator = page.iterateAll().iterator();
while (iterator.hasNext()) {
Blob remoteBlob = iterator.next();
assertEquals(BUCKET, remoteBlob.getBucket());
assertTrue(blobSet.contains(remoteBlob.getName()));
assertEquals(metadata, remoteBlob.getMetadata());
assertNull(remoteBlob.getContentType());
}
assertTrue(remoteBlob1.delete());
assertTrue(remoteBlob2.delete());
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testListBlobsCurrentDirectory.
@Test(timeout = 5000)
public void testListBlobsCurrentDirectory() throws InterruptedException {
String directoryName = "test-list-blobs-current-directory/";
String subdirectoryName = "subdirectory/";
String[] blobNames = { directoryName + subdirectoryName + "blob1", directoryName + "blob2" };
BlobInfo blob1 = BlobInfo.newBuilder(BUCKET, blobNames[0]).setContentType(CONTENT_TYPE).build();
BlobInfo blob2 = BlobInfo.newBuilder(BUCKET, blobNames[1]).setContentType(CONTENT_TYPE).build();
Blob remoteBlob1 = storage.create(blob1, BLOB_BYTE_CONTENT);
Blob remoteBlob2 = storage.create(blob2, BLOB_BYTE_CONTENT);
assertNotNull(remoteBlob1);
assertNotNull(remoteBlob2);
Page<Blob> page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-current-directory/"), Storage.BlobListOption.currentDirectory());
// test fails if timeout is reached.
while (Iterators.size(page.iterateAll().iterator()) != 2) {
Thread.sleep(500);
page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-current-directory/"), Storage.BlobListOption.currentDirectory());
}
Iterator<Blob> iterator = page.iterateAll().iterator();
while (iterator.hasNext()) {
Blob remoteBlob = iterator.next();
assertEquals(BUCKET, remoteBlob.getBucket());
if (remoteBlob.getName().equals(blobNames[1])) {
assertEquals(CONTENT_TYPE, remoteBlob.getContentType());
assertEquals(BLOB_BYTE_CONTENT.length, (long) remoteBlob.getSize());
assertFalse(remoteBlob.isDirectory());
} else if (remoteBlob.getName().equals(directoryName + subdirectoryName)) {
assertEquals(0L, (long) remoteBlob.getSize());
assertTrue(remoteBlob.isDirectory());
} else {
fail("Unexpected blob with name " + remoteBlob.getName());
}
}
assertTrue(remoteBlob1.delete());
assertTrue(remoteBlob2.delete());
}
Aggregations