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();
}
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"));
}
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)));
}
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());
}
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;
}
Aggregations