use of com.google.api.services.storage.model.StorageObject in project java-docs-samples by GoogleCloudPlatform.
the class StorageSampleTest method testUploadDelete.
@Test
public void testUploadDelete() throws Exception {
// Create a temp file to upload
Path tempPath = Files.createTempFile("StorageSampleTest", "txt");
Files.write(tempPath, ("This object is uploaded and deleted as part of the " + "StorageSampleTest integration test.").getBytes());
File tempFile = tempPath.toFile();
tempFile.deleteOnExit();
StorageSample.uploadFile(TEST_OBJECT, "text/plain", tempFile, BUCKET);
try {
// Verify that the object was created
List<StorageObject> listing = StorageSample.listBucket(BUCKET);
List<String> names = listing.stream().map(so -> so.getName()).collect(Collectors.toList());
assertThat(names).named("objects found after upload").contains(TEST_OBJECT);
} finally {
StorageSample.deleteObject(TEST_OBJECT, BUCKET);
// Verify that the object no longer exists
List<StorageObject> listing = StorageSample.listBucket(BUCKET);
List<String> names = listing.stream().map(so -> so.getName()).collect(Collectors.toList());
assertThat(names).named("objects found after delete").doesNotContain(TEST_OBJECT);
}
}
use of com.google.api.services.storage.model.StorageObject in project incubator-heron by apache.
the class GcsUploader method uploadPackage.
@Override
public URI uploadPackage() throws UploaderException {
// Backup any existing files incase we need to undo this action
final StorageObject previousStorageObject = gcsController.getStorageObject(topologyObjectName);
if (previousStorageObject != null) {
try {
gcsController.copyStorageObject(topologyObjectName, previousTopologyObjectName, previousStorageObject);
} catch (IOException ioe) {
throw new UploaderException("Failed to back up previous topology", ioe);
}
}
final StorageObject storageObject;
try {
storageObject = gcsController.createStorageObject(topologyObjectName, topologyPackageFile);
} catch (IOException ioe) {
throw new UploaderException(String.format("Error writing topology package to %s %s", bucket, topologyObjectName), ioe);
}
final String downloadUrl = getDownloadUrl(bucket, storageObject.getName());
LOG.info("Package URL: " + downloadUrl);
try {
return new URI(downloadUrl);
} catch (URISyntaxException e) {
throw new UploaderException(String.format("Could not convert URL %s to URI", downloadUrl), e);
}
}
use of com.google.api.services.storage.model.StorageObject in project incubator-heron by apache.
the class GcsUploaderTests method verifyObjectBackedUpIfExists.
@Test
public void verifyObjectBackedUpIfExists() throws IOException {
// return an object to simulate that the topology has been uploaded before
final StorageObject currentStorageObject = createStorageObject(topologyObjectName);
Mockito.when(mockGcsController.getStorageObject(Mockito.matches(topologyObjectName))).thenReturn(currentStorageObject);
// return an object when we try to create one
Mockito.when(mockGcsController.createStorageObject(Mockito.matches(topologyObjectName), Mockito.any(File.class))).thenReturn(createStorageObject(topologyObjectName));
uploader.initialize(createDefaultBuilder().build());
uploader.uploadPackage();
// verify that we copied the old topology before uploading the new one
Mockito.verify(mockGcsController).copyStorageObject(topologyObjectName, previousTopologyObjectName, currentStorageObject);
}
use of com.google.api.services.storage.model.StorageObject in project incubator-heron by apache.
the class GcsUploaderTests method createStorageObject.
private StorageObject createStorageObject(String name) {
final StorageObject storageObject = new StorageObject();
storageObject.setName(name);
return storageObject;
}
use of com.google.api.services.storage.model.StorageObject in project halyard by spinnaker.
the class GoogleWriteableProfileRegistry method writeTextObject.
private void writeTextObject(String name, String contents) {
try {
byte[] bytes = contents.getBytes();
StorageObject object = new StorageObject().setBucket(spinconfigBucket).setName(name);
ByteArrayContent content = new ByteArrayContent("application/text", bytes);
storage.objects().insert(spinconfigBucket, object, content).execute();
} catch (IOException e) {
log.error("Failed to write new object " + name, e);
throw new HalException(new ProblemBuilder(Severity.FATAL, "Failed to write to " + name + ": " + e.getMessage()).build());
}
}
Aggregations