Search in sources :

Example 1 with Bucket

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

the class CreateAndListBucketsAndBlobs method main.

public static void main(String... args) {
    // Create a service object
    // Credentials are inferred from the environment.
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // Create a bucket
    // Change this to something unique
    String bucketName = "my_unique_bucket";
    Bucket bucket = storage.create(BucketInfo.of(bucketName));
    // Upload a blob to the newly created bucket
    Blob blob = bucket.create("my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain");
    // Read the blob content from the server
    String blobContent = new String(blob.getContent(), UTF_8);
    // List all your buckets
    System.out.println("My buckets:");
    for (Bucket currentBucket : storage.list().iterateAll()) {
        System.out.println(currentBucket);
    }
    // List the blobs in a particular bucket
    System.out.println("My blobs:");
    for (Blob currentBlob : bucket.list().iterateAll()) {
        System.out.println(currentBlob);
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 2 with Bucket

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

the class ITStorageTest method testGetBucketEmptyFields.

@Test
public void testGetBucketEmptyFields() {
    Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields());
    assertEquals(BUCKET, remoteBucket.getName());
    assertNull(remoteBucket.getCreateTime());
    assertNull(remoteBucket.getSelfLink());
}
Also used : Bucket(com.google.cloud.storage.Bucket) Test(org.junit.Test)

Example 3 with Bucket

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

Example 4 with Bucket

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

the class ITStorageTest method testListBuckets.

@Test(timeout = 5000)
public void testListBuckets() throws InterruptedException {
    Iterator<Bucket> bucketIterator = storage.list(Storage.BucketListOption.prefix(BUCKET), Storage.BucketListOption.fields()).iterateAll().iterator();
    while (!bucketIterator.hasNext()) {
        Thread.sleep(500);
        bucketIterator = storage.list(Storage.BucketListOption.prefix(BUCKET), Storage.BucketListOption.fields()).iterateAll().iterator();
    }
    while (bucketIterator.hasNext()) {
        Bucket remoteBucket = bucketIterator.next();
        assertTrue(remoteBucket.getName().startsWith(BUCKET));
        assertNull(remoteBucket.getCreateTime());
        assertNull(remoteBucket.getSelfLink());
    }
}
Also used : Bucket(com.google.cloud.storage.Bucket) Test(org.junit.Test)

Example 5 with Bucket

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

the class ITStorageTest method testGetBucketSelectedFields.

@Test
public void testGetBucketSelectedFields() {
    Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
    assertEquals(BUCKET, remoteBucket.getName());
    assertNull(remoteBucket.getCreateTime());
    assertNotNull(remoteBucket.getGeneratedId());
}
Also used : Bucket(com.google.cloud.storage.Bucket) Test(org.junit.Test)

Aggregations

Bucket (com.google.cloud.storage.Bucket)9 Test (org.junit.Test)7 Blob (com.google.cloud.storage.Blob)3 Acl (com.google.cloud.storage.Acl)1 BlobInfo (com.google.cloud.storage.BlobInfo)1 BucketInfo (com.google.cloud.storage.BucketInfo)1 Storage (com.google.cloud.storage.Storage)1