Search in sources :

Example 41 with BlobInfo

use of com.google.cloud.storage.BlobInfo in project flink by apache.

the class GSBlobStorageImpl method writeBlob.

@Override
public GSBlobStorage.WriteChannel writeBlob(GSBlobIdentifier blobIdentifier, MemorySize chunkSize) {
    LOGGER.trace("Creating writeable blob for identifier {} with chunk size {}", blobIdentifier, chunkSize);
    Preconditions.checkNotNull(blobIdentifier);
    Preconditions.checkArgument(chunkSize.getBytes() > 0);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobIdentifier.getBlobId()).build();
    com.google.cloud.WriteChannel writeChannel = storage.writer(blobInfo);
    writeChannel.setChunkSize((int) chunkSize.getBytes());
    return new WriteChannel(blobIdentifier, writeChannel);
}
Also used : BlobInfo(com.google.cloud.storage.BlobInfo)

Example 42 with BlobInfo

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

the class ITStorageSnippets method testBlobAcl.

@Test
public void testBlobAcl() {
    String blobName = "test-blob-acl";
    BlobId blobId = BlobId.of(BUCKET, "test-blob-acl");
    BlobInfo blob = BlobInfo.newBuilder(blobId).build();
    Blob createdBlob = storage.create(blob);
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertNotNull(storageSnippets.createBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    Acl updatedAcl = storageSnippets.updateBlobAcl(BUCKET, blobName, createdBlob.getGeneration());
    assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
    Set<Acl> acls = Sets.newHashSet(storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
    assertTrue(acls.contains(updatedAcl));
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL));
    storage.createAcl(BlobId.of(BUCKET, blobName), Acl.of(new User(USER_EMAIL), Role.READER));
    Acl userAcl = storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL);
    assertNotNull(userAcl);
    assertEquals(USER_EMAIL, ((User) userAcl.getEntity()).getEmail());
    assertNotNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    // test non-existing blob
    String nonExistingBlob = "test-blob-acl";
    assertNull(storageSnippets.getBlobAcl(BUCKET, nonExistingBlob, 1L));
    assertFalse(storageSnippets.deleteBlobAcl(BUCKET, nonExistingBlob, 1L));
    try {
        storageSnippets.createBlobAcl(BUCKET, nonExistingBlob, 1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        storageSnippets.updateBlobAcl(BUCKET, nonExistingBlob, 1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        storageSnippets.listBlobAcls(BUCKET, nonExistingBlob, 1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
}
Also used : Blob(com.google.cloud.storage.Blob) User(com.google.cloud.storage.Acl.User) BlobInfo(com.google.cloud.storage.BlobInfo) Acl(com.google.cloud.storage.Acl) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 43 with BlobInfo

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

the class StorageSnippets method composeBlobs.

/**
 * Example of composing two blobs.
 */
// [TARGET compose(ComposeRequest)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "source_blob_1"]
// [VARIABLE "source_blob_2"]
public Blob composeBlobs(String bucketName, String blobName, String sourceBlob1, String sourceBlob2) {
    // [START composeBlobs]
    BlobId blobId = BlobId.of(bucketName, blobName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    ComposeRequest request = ComposeRequest.newBuilder().setTarget(blobInfo).addSource(sourceBlob1).addSource(sourceBlob2).build();
    Blob blob = storage.compose(request);
    // [END composeBlobs]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) ComposeRequest(com.google.cloud.storage.Storage.ComposeRequest) BlobId(com.google.cloud.storage.BlobId)

Example 44 with BlobInfo

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

the class StorageSnippets method createBlobWithSubArrayFromByteArray.

/**
 * Example of creating a blob with sub array from a byte array.
 */
// [TARGET create(BlobInfo, byte[], offset, length, BlobTargetOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public Blob createBlobWithSubArrayFromByteArray(String bucketName, String blobName, int offset, int length) {
    // [START createBlobWithSubArrayFromByteArray]
    BlobId blobId = BlobId.of(bucketName, blobName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo, "Hello, World!".getBytes(UTF_8), offset, length);
    // [END createBlobWithSubArrayFromByteArray]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Example 45 with BlobInfo

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

the class StorageSnippets method batchUpdateIterable.

/**
 * Example of updating information on several blobs using a single batch request.
 */
// [TARGET update(Iterable)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name1"]
// [VARIABLE "my_blob_name2"]
public List<Blob> batchUpdateIterable(String bucketName, String blobName1, String blobName2) {
    // [START batchUpdateIterable]
    Blob firstBlob = storage.get(bucketName, blobName1);
    Blob secondBlob = storage.get(bucketName, blobName2);
    List<BlobInfo> blobs = new LinkedList<>();
    blobs.add(firstBlob.toBuilder().setContentType("text/plain").build());
    blobs.add(secondBlob.toBuilder().setContentType("text/plain").build());
    List<Blob> updatedBlobs = storage.update(blobs);
    // [END batchUpdateIterable]
    return updatedBlobs;
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) LinkedList(java.util.LinkedList)

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