com.google.cloud.storage.StorageException Java Examples

The following examples show how to use com.google.cloud.storage.StorageException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GCSErrorWriter.java    From beast with Apache License 2.0 7 votes vote down vote up
private void storeMessagesInGCS(final List<Record> errorRecords) throws StorageException {
    //get all messages to serialize per topic
    final Map<String, GCSInvalidMessagesWrapper> topicMessagesMap = getMessagesToSerializePerTopic(errorRecords);
    //serialize the messages in GCS for each topic - a file with all messages per topic is stored in GCS
    topicMessagesMap.keySet().forEach(topicName -> {
        final String fileName = UUID.randomUUID().toString();
        final String pathPrefix = gcsBasePathPrefix + "/" + topicName + "/" + getFormattedDatePrefix(Instant.now()) + "/";
        final BlobId blobId = BlobId.of(gcsBucket, pathPrefix + fileName);
        final Map<String, String> metaDataMap = new HashMap<>();
        metaDataMap.put("topic", topicName);
        metaDataMap.put("uuid", fileName);
        final BlobInfo objectInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").setMetadata(metaDataMap).build();
        try {
            final Blob objectCreated = gcsStore.create(objectInfo, topicMessagesMap.get(topicName).getBytes());
        } catch (JsonProcessingException jpe) {
            log.error("Exception::Failed to write to GCS: {} records size: {}", jpe, errorRecords.size());
            throw new BQErrorHandlerException(jpe.getMessage());
        }
    });
    log.info("Pushing {} records to GCS success?: {}", errorRecords.size(), true);
}
 
Example #2
Source File: AbstractGCSIT.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@AfterClass
public static void tearDown() {
    try {
        // Empty the bucket before deleting it.
        Iterator<Blob> blobIterator = storage.list(BUCKET, Storage.BlobListOption.versions(true)).iterateAll();

        while(blobIterator.hasNext()) {
            Blob blob = blobIterator.next();
            storage.delete(blob.getBlobId());
        }

        storage.delete(BUCKET);
    } catch (final StorageException e) {
        fail("Unable to delete bucket " + BUCKET + ": " + e.getLocalizedMessage());
    }

    if (storage.get(BUCKET) != null) {
        fail("Incomplete teardown, subsequent tests might fail");
    }
}
 
Example #3
Source File: DeleteGCSObjectTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureOnException() throws Exception {
    reset(storage);
    final TestRunner runner = buildNewRunner(getProcessor());
    addRequiredPropertiesToRunner(runner);
    runner.assertValid();

    runner.enqueue("testdata");

    when(storage.delete(any(BlobId.class))).thenThrow(new StorageException(1, "Test Exception"));

    runner.run();

    runner.assertPenalizeCount(1);
    runner.assertAllFlowFilesTransferred(DeleteGCSObject.REL_FAILURE);
    runner.assertTransferCount(DeleteGCSObject.REL_FAILURE, 1);
}
 
Example #4
Source File: AbstractGCSIT.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() {
    try {
        helper = RemoteStorageHelper.create();
        storage = helper.getOptions().getService();

        if (storage.get(BUCKET) != null) {
            // As the generateBucketName function uses a UUID, this should pretty much never happen
            fail("Bucket " + BUCKET + " exists. Please rerun the test to generate a new bucket name.");
        }

        // Create the bucket
        storage.create(BucketInfo.of(BUCKET));
    } catch (StorageException e) {
        fail("Can't create bucket " + BUCKET + ": " + e.getLocalizedMessage());
    }

    if (storage.get(BUCKET) == null) {
        fail("Setup incomplete, tests will fail");
    }
}
 
Example #5
Source File: GenerateV4GetObjectSignedUrl.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/**
 * Signing a URL requires Credentials which implement ServiceAccountSigner. These can be set
 * explicitly using the Storage.SignUrlOption.signWith(ServiceAccountSigner) option. If you don't,
 * you could also pass a service account signer to StorageOptions, i.e.
 * StorageOptions().newBuilder().setCredentials(ServiceAccountSignerCredentials). In this example,
 * neither of these options are used, which means the following code only works when the
 * credentials are defined via the environment variable GOOGLE_APPLICATION_CREDENTIALS, and those
 * credentials are authorized to sign a URL. See the documentation for Storage.signUrl for more
 * details.
 */
public static void generateV4GetObjectSignedUrl(
    String projectId, String bucketName, String objectName) throws StorageException {
  // String projectId = "my-project-id";
  // String bucketName = "my-bucket";
  // String objectName = "my-object";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

  // Define resource
  BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build();

  URL url =
      storage.signUrl(blobInfo, 15, TimeUnit.MINUTES, Storage.SignUrlOption.withV4Signature());

  System.out.println("Generated GET signed URL:");
  System.out.println(url);
  System.out.println("You can use this URL with any user agent, for example:");
  System.out.println("curl '" + url + "'");
}
 
Example #6
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of setting a default KMS key on a bucket. */
public Bucket setDefaultKmsKey(String bucketName, String kmsKeyName) throws StorageException {
  // [START storage_set_bucket_default_kms_key]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of the existing bucket to set a default KMS key for, e.g. "my-bucket"
  // String bucketName = "my-bucket"

  // The name of the KMS-key to use as a default
  // Key names are provided in the following format:
  // 'projects/<PROJECT>/locations/<LOCATION>/keyRings/<RING_NAME>/cryptoKeys/<KEY_NAME>'
  // String kmsKeyName = ""

  BucketInfo bucketInfo =
      BucketInfo.newBuilder(bucketName).setDefaultKmsKeyName(kmsKeyName).build();

  Bucket bucket = storage.update(bucketInfo);

  System.out.println("Default KMS Key Name: " + bucket.getDefaultKmsKeyName());
  // [END storage_set_bucket_default_kms_key]
  return bucket;
}
 
Example #7
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of setting a retention policy on a bucket */
public Bucket setRetentionPolicy(String bucketName, Long retentionPeriod)
    throws StorageException {
  // [START storage_set_retention_policy]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  // The retention period for objects in bucket
  // Long retentionPeriod = 3600L; // 1 hour in seconds

  Bucket bucketWithRetentionPolicy =
      storage.update(
          BucketInfo.newBuilder(bucketName).setRetentionPeriod(retentionPeriod).build());

  System.out.println(
      "Retention period for "
          + bucketName
          + " is now "
          + bucketWithRetentionPolicy.getRetentionPeriod());
  // [END storage_set_retention_policy]
  return bucketWithRetentionPolicy;
}
 
Example #8
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of removing a retention policy on a bucket */
public Bucket removeRetentionPolicy(String bucketName)
    throws StorageException, IllegalArgumentException {
  // [START storage_remove_retention_policy]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.RETENTION_POLICY));
  if (bucket.retentionPolicyIsLocked() != null && bucket.retentionPolicyIsLocked()) {
    throw new IllegalArgumentException(
        "Unable to remove retention period as retention policy is locked.");
  }

  Bucket bucketWithoutRetentionPolicy =
      bucket.toBuilder().setRetentionPeriod(null).build().update();

  System.out.println("Retention period for " + bucketName + " has been removed");
  // [END storage_remove_retention_policy]
  return bucketWithoutRetentionPolicy;
}
 
Example #9
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to get a bucket's retention policy */
public Bucket getRetentionPolicy(String bucketName) throws StorageException {
  // [START storage_get_retention_policy]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.RETENTION_POLICY));

  System.out.println("Retention Policy for " + bucketName);
  System.out.println("Retention Period: " + bucket.getRetentionPeriod());
  if (bucket.retentionPolicyIsLocked() != null && bucket.retentionPolicyIsLocked()) {
    System.out.println("Retention Policy is locked");
  }
  if (bucket.getRetentionEffectiveTime() != null) {
    System.out.println("Effective Time: " + new Date(bucket.getRetentionEffectiveTime()));
  }
  // [END storage_get_retention_policy]
  return bucket;
}
 
Example #10
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to lock a bucket retention policy */
public Bucket lockRetentionPolicy(String bucketName) throws StorageException {
  // [START storage_lock_retention_policy]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket =
      storage.get(bucketName, Storage.BucketGetOption.fields(BucketField.METAGENERATION));
  Bucket lockedBucket =
      bucket.lockRetentionPolicy(Storage.BucketTargetOption.metagenerationMatch());

  System.out.println("Retention period for " + bucketName + " is now locked");
  System.out.println(
      "Retention policy effective as of " + new Date(lockedBucket.getRetentionEffectiveTime()));
  // [END storage_lock_retention_policy]
  return lockedBucket;
}
 
Example #11
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to enable default event-based hold for a bucket */
public Bucket enableDefaultEventBasedHold(String bucketName) throws StorageException {
  // [START storage_enable_default_event_based_hold]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket =
      storage.update(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(true).build());

  System.out.println("Default event-based hold was enabled for " + bucketName);
  // [END storage_enable_default_event_based_hold]
  return bucket;
}
 
Example #12
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to disable default event-based hold for a bucket */
public Bucket disableDefaultEventBasedHold(String bucketName) throws StorageException {
  // [START storage_disable_default_event_based_hold]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket =
      storage.update(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(false).build());

  System.out.println("Default event-based hold was disabled for " + bucketName);
  // [END storage_disable_default_event_based_hold]
  return bucket;
}
 
Example #13
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to get default event-based hold for a bucket */
public Bucket getDefaultEventBasedHold(String bucketName) throws StorageException {
  // [START storage_get_default_event_based_hold]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket =
      storage.get(bucketName, BucketGetOption.fields(BucketField.DEFAULT_EVENT_BASED_HOLD));

  if (bucket.getDefaultEventBasedHold() != null && bucket.getDefaultEventBasedHold()) {
    System.out.println("Default event-based hold is enabled for " + bucketName);
  } else {
    System.out.println("Default event-based hold is not enabled for " + bucketName);
  }
  // [END storage_get_default_event_based_hold]
  return bucket;
}
 
Example #14
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to set event-based hold for a blob */
public Blob setEventBasedHold(String bucketName, String blobName) throws StorageException {
  // [START storage_set_event_based_hold]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  // The name of a blob, e.g. "my-blob"
  // String blobName = "my-blob";

  BlobId blobId = BlobId.of(bucketName, blobName);
  Blob blob = storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(true).build());

  System.out.println("Event-based hold was set for " + blobName);
  // [END storage_set_event_based_hold]
  return blob;
}
 
Example #15
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to release event-based hold for a blob */
public Blob releaseEventBasedHold(String bucketName, String blobName) throws StorageException {
  // [START storage_release_event_based_hold]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  // The name of a blob, e.g. "my-blob"
  // String blobName = "my-blob";

  BlobId blobId = BlobId.of(bucketName, blobName);
  Blob blob = storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(false).build());

  System.out.println("Event-based hold was released for " + blobName);
  // [END storage_release_event_based_hold]
  return blob;
}
 
Example #16
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to set a temporary hold for a blob */
public Blob setTemporaryHold(String bucketName, String blobName) throws StorageException {
  // [START storage_set_temporary_hold]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  // The name of a blob, e.g. "my-blob"
  // String blobName = "my-blob";

  BlobId blobId = BlobId.of(bucketName, blobName);
  Blob blob = storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(true).build());

  System.out.println("Temporary hold was set for " + blobName);
  // [END storage_set_temporary_hold]
  return blob;
}
 
Example #17
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to release a temporary hold for a blob */
public Blob releaseTemporaryHold(String bucketName, String blobName) throws StorageException {
  // [START storage_release_temporary_hold]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  // The name of a blob, e.g. "my-blob"
  // String blobName = "my-blob";

  BlobId blobId = BlobId.of(bucketName, blobName);
  Blob blob = storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(false).build());

  System.out.println("Temporary hold was released for " + blobName);
  // [END storage_release_temporary_hold]
  return blob;
}
 
Example #18
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to enable uniform bucket-level access for a bucket */
public Bucket enableUniformBucketLevelAccess(String bucketName) throws StorageException {
  // [START storage_enable_uniform_bucket_level_access]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  BucketInfo.IamConfiguration iamConfiguration =
      BucketInfo.IamConfiguration.newBuilder().setIsUniformBucketLevelAccessEnabled(true).build();
  Bucket bucket =
      storage.update(
          BucketInfo.newBuilder(bucketName).setIamConfiguration(iamConfiguration).build());

  System.out.println("Uniform bucket-level access was enabled for " + bucketName);
  // [END storage_enable_uniform_bucket_level_access]
  return bucket;
}
 
Example #19
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to disable uniform bucket-level access for a bucket */
public Bucket disableUniformBucketLevelAccess(String bucketName) throws StorageException {
  // [START storage_disable_uniform_bucket_level_access]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  BucketInfo.IamConfiguration iamConfiguration =
      BucketInfo.IamConfiguration.newBuilder()
          .setIsUniformBucketLevelAccessEnabled(false)
          .build();
  Bucket bucket =
      storage.update(
          BucketInfo.newBuilder(bucketName).setIamConfiguration(iamConfiguration).build());

  System.out.println("Uniform bucket-level access was disabled for " + bucketName);
  // [END storage_disable_uniform_bucket_level_access]
  return bucket;
}
 
Example #20
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to get uniform bucket-level access metadata for a bucket */
public Bucket getUniformBucketLevelAccess(String bucketName) throws StorageException {
  // [START storage_get_uniform_bucket_level_access]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.IAMCONFIGURATION));
  BucketInfo.IamConfiguration iamConfiguration = bucket.getIamConfiguration();

  Boolean enabled = iamConfiguration.isUniformBucketLevelAccessEnabled();
  Date lockedTime = new Date(iamConfiguration.getUniformBucketLevelAccessLockedTime());

  if (enabled != null && enabled) {
    System.out.println("Uniform bucket-level access is enabled for " + bucketName);
    System.out.println("Bucket will be locked on " + lockedTime);
  } else {
    System.out.println("Uniform bucket-level access is disabled for " + bucketName);
  }
  // [END storage_get_uniform_bucket_level_access]
  return bucket;
}
 
Example #21
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of retrieving the metadata of an existing HMAC key. * */
public HmacKeyMetadata getHmacKey(String accessId, String projectId) throws StorageException {
  // [START storage_get_hmac_key]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The access ID of the HMAC key, e.g. "GOOG0234230X00"
  // String accessId = "GOOG0234230X00";
  //
  // The ID of the project to which the service account belongs.
  // String projectId = "project-id";
  HmacKeyMetadata metadata =
      storage.getHmacKey(accessId, Storage.GetHmacKeyOption.projectId(projectId));

  System.out.println("The HMAC key metadata is:");
  System.out.println("ID: " + metadata.getId());
  System.out.println("Access ID: " + metadata.getAccessId());
  System.out.println("Project ID: " + metadata.getProjectId());
  System.out.println("Service Account Email: " + metadata.getServiceAccount().getEmail());
  System.out.println("State: " + metadata.getState().toString());
  System.out.println("Time Created: " + new Date(metadata.getCreateTime()).toString());
  System.out.println("Time Updated: " + new Date(metadata.getUpdateTime()).toString());
  System.out.println("ETag: " + metadata.getEtag());
  // [END storage_get_hmac_key]
  return metadata;
}
 
Example #22
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of deleting an existing inactive HMAC key. * */
public void deleteHmacKey(String accessId, String projectId) throws StorageException {
  // [START storage_delete_hmac_key]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The access ID of the HMAC key, e.g. "GOOG0234230X00"
  // String accessId = "GOOG0234230X00";
  //
  // The ID of the project to which the service account belongs.
  // String projectId = "project-id";
  HmacKeyMetadata metadata =
      storage.getHmacKey(accessId, Storage.GetHmacKeyOption.projectId(projectId));
  storage.deleteHmacKey(metadata);

  System.out.println(
      "The key is deleted, though it will still appear in getHmacKeys() results given showDeletedKey is true.");
  // [END storage_delete_hmac_key]
}
 
Example #23
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public Page<HmacKeyMetadata> listHmacKeys(String projectId) throws StorageException {
  // [START storage_list_hmac_keys]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The ID of the project to which the service account belongs.
  // String projectId = "project-id";
  Page<HmacKeyMetadata> page = storage.listHmacKeys(ListHmacKeysOption.projectId(projectId));

  for (HmacKeyMetadata metadata : page.iterateAll()) {
    System.out.println("Service Account Email: " + metadata.getServiceAccount().getEmail());
    System.out.println("Access ID: " + metadata.getAccessId());
  }
  // [END storage_list_hmac_keys]
  return page;
}
 
Example #24
Source File: ITStorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateCopyAndGetBlob() {
  String blobName = "test-create-copy-get-blob";
  Blob blob = storageSnippets.createBlobFromByteArray(BUCKET, blobName);
  assertNotNull(blob);
  Blob copiedBlob = storageSnippets.copyBlobInChunks(BUCKET, blobName, "copy-blob");
  assertNotNull(copiedBlob);
  try {
    storageSnippets.getBlobFromIdWithMetageneration(BUCKET, blobName, -1);
    fail("Expected StorageException to be thrown");
  } catch (StorageException ex) {
    // expected
  }
  assertTrue(
      storageSnippets.deleteBlobFromIdWithGeneration(BUCKET, blobName, blob.getGeneration()));
  copiedBlob.delete();
}
 
Example #25
Source File: ITStorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateCopyAndGetBlobFromSubArray() {
  String blobName = "test-create-copy-get-blob-from-sub-array";
  Blob blob = storageSnippets.createBlobWithSubArrayFromByteArray(BUCKET, blobName, 7, 1);
  assertNotNull(blob);
  Blob copiedBlob = storageSnippets.copyBlobInChunks(BUCKET, blobName, "copy-blob");
  assertNotNull(copiedBlob);
  try {
    storageSnippets.getBlobFromIdWithMetageneration(BUCKET, blobName, -1);
    fail("Expected StorageException to be thrown");
  } catch (StorageException ex) {
    // expected
  }
  assertTrue(
      storageSnippets.deleteBlobFromIdWithGeneration(BUCKET, blobName, blob.getGeneration()));
  copiedBlob.delete();
}
 
Example #26
Source File: FetchGCSObjectTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testStorageExceptionOnFetch() throws Exception {
    reset(storage);
    final TestRunner runner = buildNewRunner(getProcessor());
    addRequiredPropertiesToRunner(runner);
    runner.assertValid();

    when(storage.get(any(BlobId.class))).thenThrow(new StorageException(400, "test-exception"));
    when(storage.reader(any(BlobId.class), any(Storage.BlobSourceOption.class))).thenReturn(new MockReadChannel(CONTENT));

    runner.enqueue("");

    runner.run();

    runner.assertAllFlowFilesTransferred(FetchGCSObject.REL_FAILURE);
    runner.assertTransferCount(FetchGCSObject.REL_FAILURE, 1);
}
 
Example #27
Source File: AbstractGCSIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() {
    try {
        helper = RemoteStorageHelper.create();
        storage = helper.getOptions().getService();

        if (storage.get(BUCKET) != null) {
            // As the generateBucketName function uses a UUID, this should pretty much never happen
            fail("Bucket " + BUCKET + " exists. Please rerun the test to generate a new bucket name.");
        }

        // Create the bucket
        storage.create(BucketInfo.of(BUCKET));
    } catch (StorageException e) {
        fail("Can't create bucket " + BUCKET + ": " + e.getLocalizedMessage());
    }

    if (storage.get(BUCKET) == null) {
        fail("Setup incomplete, tests will fail");
    }
}
 
Example #28
Source File: AbstractGCSIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
@AfterClass
public static void tearDown() {
    try {
        // Empty the bucket before deleting it.
        Iterable<Blob> blobIterable = storage.list(BUCKET, Storage.BlobListOption.versions(true)).iterateAll();

        for (final Blob blob : blobIterable) {
            storage.delete(blob.getBlobId());
        }

        storage.delete(BUCKET);
    } catch (final StorageException e) {
        fail("Unable to delete bucket " + BUCKET + ": " + e.getLocalizedMessage());
    }

    if (storage.get(BUCKET) != null) {
        fail("Incomplete teardown, subsequent tests might fail");
    }
}
 
Example #29
Source File: PutGCSObjectTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureHandling() throws Exception {
    reset(storage);
    final PutGCSObject processor = getProcessor();
    final TestRunner runner = buildNewRunner(processor);
    addRequiredPropertiesToRunner(runner);
    runner.assertValid();

    when(storage.create(any(BlobInfo.class), any(InputStream.class), any(Storage.BlobWriteOption.class)))
            .thenThrow(new StorageException(404, "test exception"));

    runner.enqueue("test");
    runner.run();

    runner.assertAllFlowFilesTransferred(PutGCSObject.REL_FAILURE);
    runner.assertTransferCount(PutGCSObject.REL_FAILURE, 1);

    final MockFlowFile mockFlowFile = runner.getFlowFilesForRelationship(PutGCSObject.REL_FAILURE).get(0);
    assertTrue(mockFlowFile.isPenalized());
}
 
Example #30
Source File: GCSErrorWriter.java    From beast with Apache License 2.0 6 votes vote down vote up
@Override
public Status writeErrorRecords(List<Record> errorRecords) {
    if (errorRecords.isEmpty()) {
        return new WriteStatus(true, Optional.ofNullable(null));
    }
    final Instant startTime = Instant.now();
    try {
        //Store messages in the GCS
        storeMessagesInGCS(errorRecords);
    } catch (StorageException se) {
        log.error("Exception::Failed to write to GCS: {} records size: {}", se, errorRecords.size());
        throw new BQErrorHandlerException(se.getMessage());
    }
    statsClient.timeIt("sink.gcs.push.invalid.time", startTime);
    //alter the insert status - as successful
    return new WriteStatus(true, Optional.ofNullable(null));
}