use of com.google.cloud.storage.BlobInfo 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.BlobInfo 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.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testBlobAcl.
@Test
public void testBlobAcl() {
BlobId blobId = BlobId.of(BUCKET, "test-blob-acl");
BlobInfo blob = BlobInfo.newBuilder(blobId).build();
storage.create(blob);
assertNull(storage.getAcl(blobId, User.ofAllAuthenticatedUsers()));
Acl acl = Acl.of(User.ofAllAuthenticatedUsers(), Role.READER);
assertNotNull(storage.createAcl(blobId, acl));
Acl updatedAcl = storage.updateAcl(blobId, acl.toBuilder().setRole(Role.OWNER).build());
assertEquals(Role.OWNER, updatedAcl.getRole());
Set<Acl> acls = Sets.newHashSet(storage.listAcls(blobId));
assertTrue(acls.contains(updatedAcl));
assertTrue(storage.deleteAcl(blobId, User.ofAllAuthenticatedUsers()));
assertNull(storage.getAcl(blobId, User.ofAllAuthenticatedUsers()));
// test non-existing blob
BlobId otherBlobId = BlobId.of(BUCKET, "test-blob-acl", -1L);
assertNull(storage.getAcl(otherBlobId, User.ofAllAuthenticatedUsers()));
assertFalse(storage.deleteAcl(otherBlobId, User.ofAllAuthenticatedUsers()));
try {
storage.createAcl(otherBlobId, acl);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
try {
storage.updateAcl(otherBlobId, acl);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
try {
storage.listAcls(otherBlobId);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
}
use of com.google.cloud.storage.BlobInfo 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());
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testUpdateBlobUnsetMetadata.
@Test
public void testUpdateBlobUnsetMetadata() {
String blobName = "test-update-blob-unset-metadata";
ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a", "k2", "b");
Map<String, String> newMetadata = new HashMap<>();
newMetadata.put("k1", "a");
newMetadata.put("k2", null);
ImmutableMap<String, String> expectedMetadata = ImmutableMap.of("k1", "a");
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
Blob remoteBlob = storage.create(blob);
assertNotNull(remoteBlob);
Blob updatedBlob = remoteBlob.toBuilder().setMetadata(newMetadata).build().update();
assertNotNull(updatedBlob);
assertEquals(blob.getName(), updatedBlob.getName());
assertEquals(blob.getBucket(), updatedBlob.getBucket());
assertEquals(expectedMetadata, updatedBlob.getMetadata());
assertTrue(updatedBlob.delete());
}
Aggregations