Search in sources :

Example 26 with BlobInfo

use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testUpdateBlob.

@Test
public void testUpdateBlob() {
    String blobName = "test-update-blob";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    Blob updatedBlob = remoteBlob.toBuilder().setContentType(CONTENT_TYPE).build().update();
    assertNotNull(updatedBlob);
    assertEquals(blob.getName(), updatedBlob.getName());
    assertEquals(blob.getBucket(), updatedBlob.getBucket());
    assertEquals(CONTENT_TYPE, updatedBlob.getContentType());
    assertTrue(updatedBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 27 with BlobInfo

use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testBatchRequestFail.

@Test
public void testBatchRequestFail() {
    String blobName = "test-batch-request-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    BlobInfo updatedBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build();
    StorageBatch batch = storage.batch();
    StorageBatchResult<Blob> updateResult = batch.update(updatedBlob, Storage.BlobTargetOption.generationMatch());
    StorageBatchResult<Boolean> deleteResult1 = batch.delete(BUCKET, blobName, Storage.BlobSourceOption.generationMatch(-1L));
    StorageBatchResult<Boolean> deleteResult2 = batch.delete(BlobId.of(BUCKET, blobName, -1L));
    StorageBatchResult<Blob> getResult1 = batch.get(BUCKET, blobName, Storage.BlobGetOption.generationMatch(-1L));
    StorageBatchResult<Blob> getResult2 = batch.get(BlobId.of(BUCKET, blobName, -1L));
    batch.submit();
    try {
        updateResult.get();
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        deleteResult1.get();
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    assertFalse(deleteResult2.get());
    try {
        getResult1.get();
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    assertNull(getResult2.get());
}
Also used : Blob(com.google.cloud.storage.Blob) StorageBatch(com.google.cloud.storage.StorageBatch) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 28 with BlobInfo

use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testGetBlobSelectedFields.

@Test
public void testGetBlobSelectedFields() {
    String blobName = "test-get-selected-fields-blob";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).setMetadata(ImmutableMap.of("k", "v")).build();
    assertNotNull(storage.create(blob));
    Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields(BlobField.METADATA));
    assertEquals(blob.getBlobId(), remoteBlob.getBlobId());
    assertEquals(ImmutableMap.of("k", "v"), remoteBlob.getMetadata());
    assertNull(remoteBlob.getContentType());
    assertTrue(remoteBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 29 with BlobInfo

use of com.google.cloud.storage.BlobInfo 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));
}
Also used : Blob(com.google.cloud.storage.Blob) Random(java.util.Random) WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) ByteBuffer(java.nio.ByteBuffer) StorageException(com.google.cloud.storage.StorageException) ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Example 30 with BlobInfo

use of com.google.cloud.storage.BlobInfo 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());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) BlobId(com.google.cloud.storage.BlobId) ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Aggregations

BlobInfo (com.google.cloud.storage.BlobInfo)94 Test (org.junit.Test)61 Blob (com.google.cloud.storage.Blob)56 BlobId (com.google.cloud.storage.BlobId)31 Storage (com.google.cloud.storage.Storage)21 StorageException (com.google.cloud.storage.StorageException)17 WriteChannel (com.google.cloud.WriteChannel)13 ReadChannel (com.google.cloud.ReadChannel)7 CopyWriter (com.google.cloud.storage.CopyWriter)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteBuffer (java.nio.ByteBuffer)7 InputStream (java.io.InputStream)4 URL (java.net.URL)4 StorageBatch (com.google.cloud.storage.StorageBatch)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 TestRunner (org.apache.nifi.util.TestRunner)3 Acl (com.google.cloud.storage.Acl)2 Bucket (com.google.cloud.storage.Bucket)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2