Java Code Examples for com.google.cloud.storage.Storage#delete()

The following examples show how to use com.google.cloud.storage.Storage#delete() . These examples are extracted from open source projects. 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
@After
public void destroyBucket() throws IOException {
  Storage storage = StorageOptions.newBuilder()
      .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(firestoreNativeConfiguration)))
      .build().getService();
  log.debug("Deleting files from " + bucketName);
  // must delete all the files within the bucket before we can delete the bucket
  Iterator<Blob> list = storage.list(bucketName,
      Storage.BlobListOption.prefix("")).iterateAll()
      .iterator();
  list.forEachRemaining(blob -> blob.delete());

  storage.delete(bucketName);

  log.info(bucketName + "bucket deleted");
}
 
Example 2
Source Project: java-docs-samples   File: QuickStartIT.java    License: Apache License 2.0 6 votes vote down vote up
private static final void deleteObjects() {
  Storage storage = StorageOptions.getDefaultInstance().getService();
  for (BlobInfo info :
      storage
          .list(
              bucketName,
              BlobListOption.versions(true),
              BlobListOption.currentDirectory(),
              BlobListOption.prefix(path + "/"))
          .getValues()) {
    storage.delete(info.getBlobId());
  }
}
 
Example 3
public static void deleteOldVersionOfObject(
    String projectId, String bucketName, String objectName, long generationToDelete) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

  // The ID of your GCS bucket
  // String bucketName = "your-unique-bucket-name";

  // The ID of your GCS object
  // String objectName = "your-object-name";

  // The generation of objectName to delete
  // long generationToDelete = 1579287380533984;

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  storage.delete(BlobId.of(bucketName, objectName, generationToDelete));

  System.out.println(
      "Generation "
          + generationToDelete
          + " of object "
          + objectName
          + " was deleted from "
          + bucketName);
}
 
Example 4
Source Project: metastore   File: StorageProviderTest.java    License: Apache License 2.0 5 votes vote down vote up
private void clearGcs(String bucket, String path) {
  Storage storage = StorageOptions.getDefaultInstance().getService();
  try {
    storage.delete(BlobInfo.newBuilder(bucket, path + "test.pb").build().getBlobId());
  } catch (Exception e) {
    //
  }
}
 
Example 5
@After
public void destroyBucket() throws Exception {
  Storage storage = StorageOptions.newBuilder().build().getService();
  log.debug("Deleting files from {}", bucketName);
  // must delete all the files within the bucket before we can delete the bucket
  Iterator<Blob> list = storage.list(bucketName,
      Storage.BlobListOption.prefix("")).iterateAll()
      .iterator();
  list.forEachRemaining(blob -> blob.delete());

  storage.delete(bucketName);

  log.info("{} bucket deleted", bucketName);
}
 
Example 6
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final long startNanos = System.nanoTime();

    final String bucket = context.getProperty(BUCKET)
                               .evaluateAttributeExpressions(flowFile)
                               .getValue();
    final String key = context.getProperty(KEY)
                               .evaluateAttributeExpressions(flowFile)
                               .getValue();

    final Long generation = context.getProperty(GENERATION)
            .evaluateAttributeExpressions(flowFile)
            .asLong();


    final Storage storage = getCloudService();

    // Deletes a key on Google Cloud
    try {
        storage.delete(BlobId.of(bucket, key, generation));
    } catch (Exception e) {
        getLogger().error(e.getMessage(), e);
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }

    session.transfer(flowFile, REL_SUCCESS);
    final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
    getLogger().info("Successfully deleted GCS Object for {} in {} millis; routing to success", new Object[]{flowFile, millis});
}
 
Example 7
@After
public void tearDown() throws IOException {
  ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_1);
  ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
  Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService();
  // Delete the created blob
  storage.delete(blob.getBlobId());
  System.setOut(null);
}
 
Example 8
@Override
public void run(Storage storage, BlobId... blobIds) {
  // use batch operation
  List<Boolean> deleteResults = storage.delete(blobIds);
  int index = 0;
  for (Boolean deleted : deleteResults) {
    if (deleted) {
      // request order is maintained
      System.out.printf("Blob %s was deleted%n", blobIds[index]);
    }
    index++;
  }
}
 
Example 9
Source Project: google-cloud-java   File: DeleteObject.java    License: Apache License 2.0 5 votes vote down vote up
public static void deleteObject(String projectId, String bucketName, String objectName) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

  // The ID of your GCS bucket
  // String bucketName = "your-unique-bucket-name";

  // The ID of your GCS object
  // String objectName = "your-object-name";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  storage.delete(bucketName, objectName);

  System.out.println("Object " + objectName + " was deleted from " + bucketName);
}
 
Example 10
Source Project: nifi   File: DeleteGCSObject.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final long startNanos = System.nanoTime();

    final String bucket = context.getProperty(BUCKET)
                               .evaluateAttributeExpressions(flowFile)
                               .getValue();
    final String key = context.getProperty(KEY)
                               .evaluateAttributeExpressions(flowFile)
                               .getValue();

    final Long generation = context.getProperty(GENERATION)
            .evaluateAttributeExpressions(flowFile)
            .asLong();


    final Storage storage = getCloudService();

    // Deletes a key on Google Cloud
    try {
        storage.delete(BlobId.of(bucket, key, generation));
    } catch (Exception e) {
        getLogger().error(e.getMessage(), e);
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }

    session.transfer(flowFile, REL_SUCCESS);
    final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
    getLogger().info("Successfully deleted GCS Object for {} in {} millis; routing to success", new Object[]{flowFile, millis});
}
 
Example 11
private static final void deleteBucket(String bucketName) {
  Storage storage = StorageOptions.getDefaultInstance().getService();
  storage.delete(bucketName);
}