use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testPostSignedUrl.
@Test
public void testPostSignedUrl() throws IOException {
String blobName = "test-post-signed-url-blob";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
assertNotNull(storage.create(blob));
URL url = storage.signUrl(blob, 1, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.POST));
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.connect();
Blob remoteBlob = storage.get(BUCKET, blobName);
assertNotNull(remoteBlob);
assertEquals(blob.getBucket(), remoteBlob.getBucket());
assertEquals(blob.getName(), remoteBlob.getName());
assertTrue(remoteBlob.delete());
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testListBlobsVersioned.
@Test(timeout = 15000)
public void testListBlobsVersioned() throws ExecutionException, InterruptedException {
String bucketName = RemoteStorageHelper.generateBucketName();
Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName).setVersioningEnabled(true).build());
try {
String[] blobNames = { "test-list-blobs-versioned-blob1", "test-list-blobs-versioned-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 remoteBlob2 = storage.create(blob2);
Blob remoteBlob3 = storage.create(blob2);
assertNotNull(remoteBlob1);
assertNotNull(remoteBlob2);
assertNotNull(remoteBlob3);
Page<Blob> page = storage.list(bucketName, Storage.BlobListOption.prefix("test-list-blobs-versioned-blob"), Storage.BlobListOption.versions(true));
// test fails if timeout is reached.
while (Iterators.size(page.iterateAll().iterator()) != 3) {
Thread.sleep(500);
page = storage.list(bucketName, Storage.BlobListOption.prefix("test-list-blobs-versioned-blob"), Storage.BlobListOption.versions(true));
}
Set<String> blobSet = ImmutableSet.of(blobNames[0], blobNames[1]);
Iterator<Blob> iterator = page.iterateAll().iterator();
while (iterator.hasNext()) {
Blob remoteBlob = iterator.next();
assertEquals(bucketName, remoteBlob.getBucket());
assertTrue(blobSet.contains(remoteBlob.getName()));
assertNotNull(remoteBlob.getGeneration());
}
assertTrue(remoteBlob1.delete());
assertTrue(remoteBlob2.delete());
assertTrue(remoteBlob3.delete());
} finally {
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
}
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testCopyBlobUpdateStorageClass.
@Test
public void testCopyBlobUpdateStorageClass() {
String sourceBlobName = "test-copy-blob-update-storage-class-source";
BlobId source = BlobId.of(BUCKET, sourceBlobName);
BlobInfo sourceInfo = BlobInfo.newBuilder(source).setStorageClass(StorageClass.STANDARD).build();
Blob remoteSourceBlob = storage.create(sourceInfo, BLOB_BYTE_CONTENT);
assertNotNull(remoteSourceBlob);
assertEquals(StorageClass.STANDARD, remoteSourceBlob.getStorageClass());
String targetBlobName = "test-copy-blob-update-storage-class-target";
BlobInfo targetInfo = BlobInfo.newBuilder(BUCKET, targetBlobName).setStorageClass(StorageClass.COLDLINE).build();
Storage.CopyRequest req = Storage.CopyRequest.of(source, targetInfo);
CopyWriter copyWriter = storage.copy(req);
assertEquals(BUCKET, copyWriter.getResult().getBucket());
assertEquals(targetBlobName, copyWriter.getResult().getName());
assertEquals(StorageClass.COLDLINE, copyWriter.getResult().getStorageClass());
assertTrue(copyWriter.isDone());
assertTrue(remoteSourceBlob.delete());
assertTrue(storage.delete(BUCKET, targetBlobName));
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testUpdateBlobMergeMetadata.
@Test
public void testUpdateBlobMergeMetadata() {
String blobName = "test-update-blob-merge-metadata";
ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a");
ImmutableMap<String, String> newMetadata = ImmutableMap.of("k2", "b");
ImmutableMap<String, String> expectedMetadata = ImmutableMap.of("k1", "a", "k2", "b");
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());
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testComposeBlobFail.
@Test
public void testComposeBlobFail() {
String sourceBlobName1 = "test-compose-blob-fail-source-1";
String sourceBlobName2 = "test-compose-blob-fail-source-2";
BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
Blob remoteSourceBlob1 = storage.create(sourceBlob1);
Blob remoteSourceBlob2 = storage.create(sourceBlob2);
assertNotNull(remoteSourceBlob1);
assertNotNull(remoteSourceBlob2);
String targetBlobName = "test-compose-blob-fail-target";
BlobInfo targetBlob = BlobInfo.newBuilder(BUCKET, targetBlobName).build();
Storage.ComposeRequest req = Storage.ComposeRequest.newBuilder().addSource(sourceBlobName1, -1L).addSource(sourceBlobName2, -1L).setTarget(targetBlob).build();
try {
storage.compose(req);
fail("StorageException was expected");
} catch (StorageException ex) {
// expected
}
assertTrue(remoteSourceBlob1.delete());
assertTrue(remoteSourceBlob2.delete());
}
Aggregations