use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testDeleteBlobsFail.
@Test
public void testDeleteBlobsFail() {
String sourceBlobName1 = "test-delete-blobs-fail-1";
String sourceBlobName2 = "test-delete-blobs-fail-2";
BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
assertNotNull(storage.create(sourceBlob1));
List<Boolean> deleteStatus = storage.delete(sourceBlob1.getBlobId(), sourceBlob2.getBlobId());
assertTrue(deleteStatus.get(0));
assertFalse(deleteStatus.get(1));
}
use of com.google.cloud.storage.BlobInfo 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.BlobInfo 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.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadAndWriteChannels.
@Test
public void testReadAndWriteChannels() throws IOException {
String blobName = "test-read-and-write-channels-blob";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
byte[] stringBytes;
try (WriteChannel writer = storage.writer(blob)) {
stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
writer.write(ByteBuffer.wrap(stringBytes));
}
ByteBuffer readBytes;
ByteBuffer readStringBytes;
try (ReadChannel reader = storage.reader(blob.getBlobId())) {
readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length);
readStringBytes = ByteBuffer.allocate(stringBytes.length);
reader.read(readBytes);
reader.read(readStringBytes);
}
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array());
assertEquals(BLOB_STRING_CONTENT, new String(readStringBytes.array(), UTF_8));
assertTrue(storage.delete(BUCKET, blobName));
}
use of com.google.cloud.storage.BlobInfo 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));
}
Aggregations