Search in sources :

Example 36 with Blob

use of com.google.cloud.storage.Blob 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 37 with Blob

use of com.google.cloud.storage.Blob 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)

Example 38 with Blob

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

Example 39 with Blob

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

Example 40 with Blob

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

Aggregations

Blob (com.google.cloud.storage.Blob)80 BlobInfo (com.google.cloud.storage.BlobInfo)50 Test (org.junit.Test)50 BlobId (com.google.cloud.storage.BlobId)23 StorageException (com.google.cloud.storage.StorageException)16 Storage (com.google.cloud.storage.Storage)12 CopyWriter (com.google.cloud.storage.CopyWriter)10 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)7 StorageBatch (com.google.cloud.storage.StorageBatch)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 ReadChannel (com.google.cloud.ReadChannel)3 Acl (com.google.cloud.storage.Acl)3 Bucket (com.google.cloud.storage.Bucket)3 CopyRequest (com.google.cloud.storage.Storage.CopyRequest)3 HashMap (java.util.HashMap)3 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 FileInputStream (java.io.FileInputStream)2 ByteBuffer (java.nio.ByteBuffer)2