Search in sources :

Example 1 with BlobInfo

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

the class CloudStorageHelper method uploadFile.

// [END init]
// [START uploadFile]
/**
 * Uploads a file to Google Cloud Storage to the bucket specified in the BUCKET_NAME
 * environment variable, appending a timestamp to end of the uploaded filename.
 */
@SuppressWarnings("deprecation")
public String uploadFile(Part filePart, final String bucketName) throws IOException {
    DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
    DateTime dt = DateTime.now(DateTimeZone.UTC);
    String dtString = dt.toString(dtf);
    final String fileName = filePart.getSubmittedFileName() + dtString;
    // the inputstream is closed by default, so we don't need to close it here
    BlobInfo blobInfo = storage.create(BlobInfo.newBuilder(bucketName, fileName).setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER)))).build(), filePart.getInputStream());
    logger.log(Level.INFO, "Uploaded file {0} as {1}", new Object[] { filePart.getSubmittedFileName(), fileName });
    // return the public download link
    return blobInfo.getMediaLink();
}
Also used : BlobInfo(com.google.cloud.storage.BlobInfo) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime)

Example 2 with BlobInfo

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

the class PutGCSObjectTest method testSuccessfulPutOperationWithUserMetadata.

@Test
public void testSuccessfulPutOperationWithUserMetadata() throws Exception {
    reset(storage, blob);
    final PutGCSObject processor = getProcessor();
    final TestRunner runner = buildNewRunner(processor);
    addRequiredPropertiesToRunner(runner);
    runner.setProperty("testMetadataKey1", "testMetadataValue1");
    runner.setProperty("testMetadataKey2", "testMetadataValue2");
    runner.assertValid();
    when(storage.create(blobInfoArgumentCaptor.capture(), inputStreamArgumentCaptor.capture(), blobWriteOptionArgumentCaptor.capture())).thenReturn(blob);
    runner.enqueue("test");
    runner.run();
    runner.assertAllFlowFilesTransferred(PutGCSObject.REL_SUCCESS);
    runner.assertTransferCount(PutGCSObject.REL_SUCCESS, 1);
    /*
        String text;
        try (final Reader reader = new InputStreamReader(inputStreamArgumentCaptor.getValue())) {
            text = CharStreams.toString(reader);
        }

        assertEquals(
                "FlowFile content should be equal to the Blob content",
                "test",
                text
        );

        */
    final BlobInfo blobInfo = blobInfoArgumentCaptor.getValue();
    final Map<String, String> metadata = blobInfo.getMetadata();
    assertNotNull(metadata);
    assertEquals(2, metadata.size());
    assertEquals("testMetadataValue1", metadata.get("testMetadataKey1"));
    assertEquals("testMetadataValue2", metadata.get("testMetadataKey2"));
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 3 with BlobInfo

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

the class PutGCSObjectTest method testSuccessfulPutOperation.

@Test
public void testSuccessfulPutOperation() throws Exception {
    reset(storage, blob);
    final PutGCSObject processor = getProcessor();
    final TestRunner runner = buildNewRunner(processor);
    addRequiredPropertiesToRunner(runner);
    runner.setProperty(PutGCSObject.KEY, KEY);
    runner.setProperty(PutGCSObject.CONTENT_TYPE, CONTENT_TYPE);
    runner.setProperty(PutGCSObject.MD5, MD5);
    runner.setProperty(PutGCSObject.CRC32C, CRC32C);
    runner.setProperty(PutGCSObject.ACL, ACL.name());
    runner.setProperty(PutGCSObject.ENCRYPTION_KEY, ENCRYPTION_KEY);
    runner.setProperty(PutGCSObject.OVERWRITE, String.valueOf(OVERWRITE));
    runner.setProperty(PutGCSObject.CONTENT_DISPOSITION_TYPE, CONTENT_DISPOSITION_TYPE);
    runner.assertValid();
    when(storage.create(blobInfoArgumentCaptor.capture(), inputStreamArgumentCaptor.capture(), blobWriteOptionArgumentCaptor.capture())).thenReturn(blob);
    runner.enqueue("test", ImmutableMap.of(CoreAttributes.FILENAME.key(), FILENAME));
    runner.run();
    runner.assertAllFlowFilesTransferred(PutGCSObject.REL_SUCCESS);
    runner.assertTransferCount(PutGCSObject.REL_SUCCESS, 1);
    /*

        String text;
        try (final Reader reader = new InputStreamReader(inputStreamArgumentCaptor.getValue())) {
            text = CharStreams.toString(reader);
        }

        assertEquals(
                "FlowFile content should be equal to the Blob content",
                "test",
                text
        );

        */
    final BlobInfo blobInfo = blobInfoArgumentCaptor.getValue();
    assertEquals(BUCKET, blobInfo.getBucket());
    assertEquals(KEY, blobInfo.getName());
    assertEquals(CONTENT_DISPOSITION_TYPE + "; filename=" + FILENAME, blobInfo.getContentDisposition());
    assertEquals(CONTENT_TYPE, blobInfo.getContentType());
    assertEquals(MD5, blobInfo.getMd5());
    assertEquals(CRC32C, blobInfo.getCrc32c());
    assertNull(blobInfo.getMetadata());
    final List<Storage.BlobWriteOption> blobWriteOptions = blobWriteOptionArgumentCaptor.getAllValues();
    final Set<Storage.BlobWriteOption> blobWriteOptionSet = ImmutableSet.copyOf(blobWriteOptions);
    assertEquals("Each of the BlobWriteOptions should be unique", blobWriteOptions.size(), blobWriteOptionSet.size());
    assertTrue("The doesNotExist BlobWriteOption should be set if OVERWRITE is false", blobWriteOptionSet.contains(Storage.BlobWriteOption.doesNotExist()));
    assertTrue("The md5Match BlobWriteOption should be set if MD5 is non-null", blobWriteOptionSet.contains(Storage.BlobWriteOption.md5Match()));
    assertTrue("The crc32cMatch BlobWriteOption should be set if CRC32C is non-null", blobWriteOptionSet.contains(Storage.BlobWriteOption.crc32cMatch()));
    assertTrue("The predefinedAcl BlobWriteOption should be set if ACL is non-null", blobWriteOptionSet.contains(Storage.BlobWriteOption.predefinedAcl(ACL)));
    assertTrue("The encryptionKey BlobWriteOption should be set if ENCRYPTION_KEY is non-null", blobWriteOptionSet.contains(Storage.BlobWriteOption.encryptionKey(ENCRYPTION_KEY)));
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 4 with BlobInfo

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

the class PutGCSObjectTest method testSuccessfulPutOperationNoParameters.

@Test
public void testSuccessfulPutOperationNoParameters() throws Exception {
    reset(storage, blob);
    final PutGCSObject processor = getProcessor();
    final TestRunner runner = buildNewRunner(processor);
    addRequiredPropertiesToRunner(runner);
    runner.assertValid();
    when(storage.create(blobInfoArgumentCaptor.capture(), inputStreamArgumentCaptor.capture(), blobWriteOptionArgumentCaptor.capture())).thenReturn(blob);
    runner.enqueue("test");
    runner.run();
    runner.assertAllFlowFilesTransferred(PutGCSObject.REL_SUCCESS);
    runner.assertTransferCount(PutGCSObject.REL_SUCCESS, 1);
    /**
     * Can't do this any more due to the switch to Java InputStreams which close after an operation *
     */
    /*
        String text;
        try (final Reader reader = new InputStreamReader(inputStreamArgumentCaptor.getValue())) {
            text = CharStreams.toString(reader);
        }

        assertEquals(
                "FlowFile content should be equal to the Blob content",
                "test",
                text
        );
        */
    final List<Storage.BlobWriteOption> blobWriteOptions = blobWriteOptionArgumentCaptor.getAllValues();
    assertEquals("No BlobWriteOptions should be set", 0, blobWriteOptions.size());
    final BlobInfo blobInfo = blobInfoArgumentCaptor.getValue();
    assertNull(blobInfo.getMd5());
    assertNull(blobInfo.getContentDisposition());
    assertNull(blobInfo.getCrc32c());
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 5 with BlobInfo

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

the class StorageSnippets method createEncryptedBlob.

/**
   * Example of uploading an encrypted blob.
   */
// [TARGET create(BlobInfo, InputStream, BlobWriteOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "my_encryption_key"]
public Blob createEncryptedBlob(String bucketName, String blobName, String encryptionKey) {
    // [START storageUploadEncryptedFile]
    InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
    BlobId blobId = BlobId.of(bucketName, blobName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo, content, BlobWriteOption.encryptionKey(encryptionKey));
    // [END storageUploadEncryptedFile]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) 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