Search in sources :

Example 91 with BlobInfo

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

the class ITStorageSnippets method testComposeBlobs.

@Test
public void testComposeBlobs() {
    String blobName = "my_blob_name";
    String sourceBlobName1 = "source_blob_1";
    String sourceBlobName2 = "source_blob_2";
    BlobInfo blobInfo1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
    BlobInfo blobInfo2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
    storage.create(blobInfo1);
    storage.create(blobInfo2);
    assertNotNull(storageSnippets.composeBlobs(BUCKET, blobName, sourceBlobName1, sourceBlobName2));
}
Also used : BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 92 with BlobInfo

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

the class ITBlobSnippets method testGetObjectMetadata.

@Test
public void testGetObjectMetadata() {
    String blobName = "test-create-empty-blob";
    BlobId blobId = BlobId.of(BUCKET, blobName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setMetadata(ImmutableMap.of("k", "v")).build();
    Blob remoteBlob = storage.create(blobInfo, CONTENT);
    assertNotNull(remoteBlob);
    PrintStream systemOut = System.out;
    final ByteArrayOutputStream snippetOutputCapture = new ByteArrayOutputStream();
    System.setOut(new PrintStream(snippetOutputCapture));
    GetObjectMetadata.getObjectMetadata(PROJECT_ID, BUCKET, blobName);
    String snippetOutput = snippetOutputCapture.toString();
    System.setOut(systemOut);
    assertTrue(snippetOutput.contains("Bucket: " + remoteBlob.getBucket()));
    assertTrue(snippetOutput.contains("Bucket: " + remoteBlob.getBucket()));
    assertTrue(snippetOutput.contains("CacheControl: " + remoteBlob.getCacheControl()));
    assertTrue(snippetOutput.contains("ComponentCount: " + remoteBlob.getComponentCount()));
    assertTrue(snippetOutput.contains("ContentDisposition: " + remoteBlob.getContentDisposition()));
    assertTrue(snippetOutput.contains("ContentEncoding: " + remoteBlob.getContentEncoding()));
    assertTrue(snippetOutput.contains("ContentLanguage: " + remoteBlob.getContentLanguage()));
    assertTrue(snippetOutput.contains("ContentType: " + remoteBlob.getContentType()));
    assertTrue(snippetOutput.contains("CustomTime: " + remoteBlob.getCustomTime()));
    assertTrue(snippetOutput.contains("Crc32c: " + remoteBlob.getCrc32c()));
    assertTrue(snippetOutput.contains("Crc32cHexString: " + remoteBlob.getCrc32cToHexString()));
    assertTrue(snippetOutput.contains("ETag: " + remoteBlob.getEtag()));
    assertTrue(snippetOutput.contains("Generation: " + remoteBlob.getGeneration()));
    assertTrue(snippetOutput.contains("Id: " + remoteBlob.getBlobId()));
    assertTrue(snippetOutput.contains("KmsKeyName: " + remoteBlob.getKmsKeyName()));
    assertTrue(snippetOutput.contains("Md5Hash: " + remoteBlob.getMd5()));
    assertTrue(snippetOutput.contains("Md5HexString: " + remoteBlob.getMd5ToHexString()));
    assertTrue(snippetOutput.contains("MediaLink: " + remoteBlob.getMediaLink()));
    assertTrue(snippetOutput.contains("Metageneration: " + remoteBlob.getMetageneration()));
    assertTrue(snippetOutput.contains("Name: " + remoteBlob.getName()));
    assertTrue(snippetOutput.contains("Size: " + remoteBlob.getSize()));
    assertTrue(snippetOutput.contains("StorageClass: " + remoteBlob.getStorageClass()));
    assertTrue(snippetOutput.contains("TimeCreated: " + new Date(remoteBlob.getCreateTime())));
    assertTrue(snippetOutput.contains("Last Metadata Update: " + new Date(remoteBlob.getUpdateTime())));
    assertTrue(snippetOutput.contains("temporaryHold: disabled"));
    assertTrue(snippetOutput.contains("eventBasedHold: disabled"));
    assertTrue(snippetOutput.contains("User metadata:"));
    assertTrue(snippetOutput.contains("k=v"));
}
Also used : PrintStream(java.io.PrintStream) Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BlobId(com.google.cloud.storage.BlobId) Date(java.util.Date) Test(org.junit.Test)

Example 93 with BlobInfo

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

the class StorageSnippets method writer.

/**
 * Example of writing a blob's content through a writer.
 */
// [TARGET writer(BlobInfo, BlobWriteOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public void writer(String bucketName, String blobName) throws IOException {
    // [START writer]
    BlobId blobId = BlobId.of(bucketName, blobName);
    byte[] content = "Hello, World!".getBytes(UTF_8);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    try (WriteChannel writer = storage.writer(blobInfo)) {
        try {
            writer.write(ByteBuffer.wrap(content, 0, content.length));
        } catch (Exception ex) {
        // handle exception
        }
    }
// [END writer]
}
Also used : WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId) IOException(java.io.IOException) StorageException(com.google.cloud.storage.StorageException)

Example 94 with BlobInfo

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

the class StorageSnippets method createBlob.

/**
 * Example of creating a blob with no content.
 */
// [TARGET create(BlobInfo, BlobTargetOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public Blob createBlob(String bucketName, String blobName) {
    // [START createBlob]
    BlobId blobId = BlobId.of(bucketName, blobName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo);
    // [END createBlob]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

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