use of com.google.cloud.storage.BlobId in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testCopyBlob.
@Test
public void testCopyBlob() {
String sourceBlobName = "test-copy-blob-source";
BlobId source = BlobId.of(BUCKET, sourceBlobName);
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
BlobInfo blob = BlobInfo.newBuilder(source).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT);
assertNotNull(remoteBlob);
String targetBlobName = "test-copy-blob-target";
Storage.CopyRequest req = Storage.CopyRequest.of(source, BlobId.of(BUCKET, targetBlobName));
CopyWriter copyWriter = storage.copy(req);
assertEquals(BUCKET, copyWriter.getResult().getBucket());
assertEquals(targetBlobName, copyWriter.getResult().getName());
assertEquals(CONTENT_TYPE, copyWriter.getResult().getContentType());
assertEquals(metadata, copyWriter.getResult().getMetadata());
assertTrue(copyWriter.isDone());
assertTrue(remoteBlob.delete());
assertTrue(storage.delete(BUCKET, targetBlobName));
}
use of com.google.cloud.storage.BlobId 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.BlobId 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.BlobId 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.BlobId in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testBatchRequestManyOperations.
@Test
public void testBatchRequestManyOperations() {
List<StorageBatchResult<Boolean>> deleteResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE);
List<StorageBatchResult<Blob>> getResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
List<StorageBatchResult<Blob>> updateResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
StorageBatch batch = storage.batch();
for (int i = 0; i < MAX_BATCH_SIZE; i++) {
BlobId blobId = BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i);
deleteResults.add(batch.delete(blobId));
}
for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
BlobId blobId = BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i);
getResults.add(batch.get(blobId));
}
for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
BlobInfo blob = BlobInfo.newBuilder(BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i)).build();
updateResults.add(batch.update(blob));
}
String sourceBlobName1 = "test-batch-request-many-operations-source-blob-1";
String sourceBlobName2 = "test-batch-request-many-operations-source-blob-2";
BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
assertNotNull(storage.create(sourceBlob1));
assertNotNull(storage.create(sourceBlob2));
BlobInfo updatedBlob2 = sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build();
StorageBatchResult<Blob> getResult = batch.get(BUCKET, sourceBlobName1);
StorageBatchResult<Blob> updateResult = batch.update(updatedBlob2);
batch.submit();
// Check deletes
for (StorageBatchResult<Boolean> failedDeleteResult : deleteResults) {
assertFalse(failedDeleteResult.get());
}
// Check gets
for (StorageBatchResult<Blob> failedGetResult : getResults) {
assertNull(failedGetResult.get());
}
Blob remoteBlob1 = getResult.get();
assertEquals(sourceBlob1.getBucket(), remoteBlob1.getBucket());
assertEquals(sourceBlob1.getName(), remoteBlob1.getName());
// Check updates
for (StorageBatchResult<Blob> failedUpdateResult : updateResults) {
try {
failedUpdateResult.get();
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
}
Blob remoteUpdatedBlob2 = updateResult.get();
assertEquals(sourceBlob2.getBucket(), remoteUpdatedBlob2.getBucket());
assertEquals(sourceBlob2.getName(), remoteUpdatedBlob2.getName());
assertEquals(updatedBlob2.getContentType(), remoteUpdatedBlob2.getContentType());
assertTrue(remoteBlob1.delete());
assertTrue(remoteUpdatedBlob2.delete());
}
Aggregations