Search in sources :

Example 56 with StorageObject

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);
    }
}
Also used : Path(java.nio.file.Path) List(java.util.List) Bucket(com.google.api.services.storage.model.Bucket) Files(java.nio.file.Files) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Path(java.nio.file.Path) Collectors(java.util.stream.Collectors) File(java.io.File) StorageObject(com.google.api.services.storage.model.StorageObject) StorageObject(com.google.api.services.storage.model.StorageObject) File(java.io.File) Test(org.junit.Test)

Example 57 with StorageObject

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);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) UploaderException(com.twitter.heron.spi.uploader.UploaderException) URI(java.net.URI)

Example 58 with StorageObject

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);
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) File(java.io.File) Test(org.junit.Test)

Example 59 with StorageObject

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;
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject)

Example 60 with 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());
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) ByteArrayContent(com.google.api.client.http.ByteArrayContent) ProblemBuilder(com.netflix.spinnaker.halyard.core.problem.v1.ProblemBuilder)

Aggregations

StorageObject (com.google.api.services.storage.model.StorageObject)81 Test (org.junit.Test)33 Objects (com.google.api.services.storage.model.Objects)21 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)16 Storage (com.google.api.services.storage.Storage)12 List (java.util.List)9 URI (java.net.URI)8 RetryHelperException (com.google.cloud.RetryHelper.RetryHelperException)7 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)7 GcsPath (org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath)7 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)6 Nullable (javax.annotation.Nullable)6 File (java.io.File)5 Pattern (java.util.regex.Pattern)5 DateTime (com.google.api.client.util.DateTime)4 BlobSourceOption (com.google.cloud.storage.Storage.BlobSourceOption)4 BlobTargetOption (com.google.cloud.storage.Storage.BlobTargetOption)4 BlobWriteOption (com.google.cloud.storage.Storage.BlobWriteOption)4 BucketSourceOption (com.google.cloud.storage.Storage.BucketSourceOption)4