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

The following examples show how to use com.google.cloud.storage.Blob#delete() . 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: GCPBucketService.java    From cassandra-backup with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(final String bucketName) {
    final Bucket bucket = storage.get(bucketName);

    if (bucket != null) {
        for (final Blob blob : bucket.list().iterateAll()) {
            if (!blob.delete()) {
                throw new GCPModuleException(format("Blob %s was not deleted!", blob.getName()));
            }
        }

        if (!storage.delete(bucket.getName())) {
            throw new GCPModuleException(format("GCP bucket %s was not deleted!", bucket.getName()));
        }
    }
}
 
Example 2
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 3
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 4
Source File: DetectIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncBatchAnnotateImagesGcs() throws Exception {
  // Act
  AsyncBatchAnnotateImagesGcs.asyncBatchAnnotateImagesGcs(
      "gs://cloud-samples-data/vision/label/wakeupcat.jpg",
      "gs://" + OUTPUT_BUCKET + "/" + OUTPUT_PREFIX + "/");

  // Assert
  String got = bout.toString();
  assertThat(got).contains("red:");

  Storage storage = StorageOptions.getDefaultInstance().getService();

  Page<Blob> blobs = storage.list(OUTPUT_BUCKET, BlobListOption.currentDirectory(),
      BlobListOption.prefix(OUTPUT_PREFIX + "/"));
  for (Blob blob : blobs.iterateAll()) {
    blob.delete();
  }
}
 
Example 5
Source File: DetectIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testDetectDocumentsGcs() throws Exception {
  // Act
  Detect.detectDocumentsGcs(
      "gs://" + ASSET_BUCKET + "/vision/document/custom_0773375000.pdf",
      "gs://" + OUTPUT_BUCKET + "/" + OUTPUT_PREFIX + "/");

  // Assert
  String got = bout.toString();

  assertThat(got).contains("OIL, GAS AND MINERAL LEASE");

  Storage storage = StorageOptions.getDefaultInstance().getService();

  Page<Blob> blobs =
      storage.list(
          OUTPUT_BUCKET,
          BlobListOption.currentDirectory(),
          BlobListOption.prefix(OUTPUT_PREFIX + "/"));
  for (Blob blob : blobs.iterateAll()) {
    blob.delete();
  }
}
 
Example 6
Source File: BatchTranslateTextWithModelTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void deleteDirectory(Storage storage, Page<Blob> blobs) {
  for (Blob blob : blobs.iterateAll()) {
    System.out.println(blob.getBlobId());
    if (!blob.delete()) {
      Page<Blob> subBlobs =
          storage.list(
              PROJECT_ID,
              Storage.BlobListOption.currentDirectory(),
              Storage.BlobListOption.prefix(blob.getName()));

      deleteDirectory(storage, subBlobs);
    }
  }
}
 
Example 7
Source File: BatchTranslateTextTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void deleteDirectory(Storage storage, Page<Blob> blobs) {
  for (Blob blob : blobs.iterateAll()) {
    System.out.println(blob.getBlobId());
    if (!blob.delete()) {
      Page<Blob> subBlobs =
          storage.list(
              PROJECT_ID,
              Storage.BlobListOption.currentDirectory(),
              Storage.BlobListOption.prefix(blob.getName()));

      deleteDirectory(storage, subBlobs);
    }
  }
}
 
Example 8
Source File: BatchTranslateTextWithGlossaryAndModelTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void deleteDirectory(Storage storage, Page<Blob> blobs) {
  for (Blob blob : blobs.iterateAll()) {
    System.out.println(blob.getBlobId());
    if (!blob.delete()) {
      Page<Blob> subBlobs =
          storage.list(
              PROJECT_ID,
              Storage.BlobListOption.currentDirectory(),
              Storage.BlobListOption.prefix(blob.getName()));

      deleteDirectory(storage, subBlobs);
    }
  }
}
 
Example 9
Source File: BatchTranslateTextWithGlossaryTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void deleteDirectory(Storage storage, Page<Blob> blobs) {
  for (Blob blob : blobs.iterateAll()) {
    System.out.println(blob.getBlobId());
    if (!blob.delete()) {
      Page<Blob> subBlobs =
          storage.list(
              PROJECT_ID,
              Storage.BlobListOption.currentDirectory(),
              Storage.BlobListOption.prefix(blob.getName()));

      deleteDirectory(storage, subBlobs);
    }
  }
}
 
Example 10
Source File: GcsSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Before
@After
public void cleanupCloudStorage() {
	Page<Blob> blobs = this.storage.list(this.bucketName);
	for (Blob blob : blobs.iterateAll()) {
		blob.delete();
	}
}
 
Example 11
Source File: AsyncBatchAnnotateImagesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() {
  System.setOut(null);

  Storage storage = StorageOptions.getDefaultInstance().getService();

  Page<Blob> blobs =
      storage.list(
          PROJECT_ID,
          Storage.BlobListOption.currentDirectory(),
          Storage.BlobListOption.prefix(PREFIX));
  for (Blob blob : blobs.iterateAll()) {
    blob.delete();
  }
}
 
Example 12
Source File: StreamingAnnotationToStorageIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private void deleteDirectory(Storage storage, Page<Blob> blobs) {
  for (Blob blob : blobs.iterateAll()) {
    System.out.println(blob.getName());
    if (!blob.delete()) {
      Page<Blob> subBlobs =
          storage.list(
              PROJECT_ID,
              BlobListOption.currentDirectory(),
              BlobListOption.prefix(blob.getName()));

      deleteDirectory(storage, subBlobs);
    }
  }
}
 
Example 13
Source File: MoveObject.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void moveObject(
    String projectId, String sourceBucketName, String objectName, String targetBucketName) {
  // 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 ID of the bucket to move the object objectName to
  // String targetBucketName = "target-object-bucket"

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  Blob blob = storage.get(sourceBucketName, objectName);
  // Write a copy of the object to the target bucket
  CopyWriter copyWriter = blob.copyTo(targetBucketName, objectName);
  Blob copiedBlob = copyWriter.getResult();
  // Delete the original blob now that we've copied to where we want it, finishing the "move"
  // operation
  blob.delete();

  System.out.println(
      "Moved object "
          + objectName
          + " from bucket "
          + sourceBucketName
          + " to "
          + copiedBlob.getBucket());
}
 
Example 14
Source File: ITTranslateSnippetsBeta.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
private void deleteDirectory(Storage storage, Page<Blob> blobs) {
  for (Blob blob : blobs.iterateAll()) {
    System.out.println(blob.getBlobId());
    if (!blob.delete()) {
      Page<Blob> subBlobs =
          storage.list(
              projectId,
              BlobListOption.currentDirectory(),
              BlobListOption.prefix(blob.getName()));

      deleteDirectory(storage, subBlobs);
    }
  }
}
 
Example 15
Source File: StorageClientIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloudStorageSignUrl() throws IOException {
  StorageClient storage = StorageClient.getInstance(IntegrationTestUtils.ensureDefaultApp());
  Bucket bucket = storage.bucket();
  Blob blob = createTextBlob(bucket, "Signed URL Test");
  URL url = blob.signUrl(3600, TimeUnit.SECONDS);
  try (InputStream in = url.openStream()) {
    String result = CharStreams.toString(new InputStreamReader(in));
    assertEquals("Signed URL Test", result);
  } finally {
    blob.delete();
  }
}
 
Example 16
Source File: ITStorageSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadWriteAndSignUrl() throws IOException {
  String blobName = "text-read-write-sign-url";
  byte[] content = "Hello, World!".getBytes(UTF_8);
  Blob blob = storage.create(BlobInfo.newBuilder(BUCKET, blobName).build(), content);
  assertArrayEquals(
      content, storageSnippets.readBlobFromId(BUCKET, blobName, blob.getGeneration()));
  assertArrayEquals(
      content,
      storageSnippets.readBlobFromStringsWithGeneration(BUCKET, blobName, blob.getGeneration()));
  storageSnippets.readerFromId(BUCKET, blobName);
  storageSnippets.readerFromStrings(BUCKET, blobName);
  storageSnippets.writer(BUCKET, blobName);
  URL signedUrl = storageSnippets.signUrl(BUCKET, blobName);
  URLConnection connection = signedUrl.openConnection();
  byte[] readBytes = new byte[content.length];
  try (InputStream responseStream = connection.getInputStream()) {
    assertEquals(content.length, responseStream.read(readBytes));
    assertArrayEquals(content, readBytes);
  }
  signedUrl =
      storageSnippets.signUrlWithSigner(
          BUCKET, blobName, System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
  connection = signedUrl.openConnection();
  try (InputStream responseStream = connection.getInputStream()) {
    assertEquals(content.length, responseStream.read(readBytes));
    assertArrayEquals(content, readBytes);
  }
  blob.delete();
}
 
Example 17
Source File: GcsSpringIntegrationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
private void cleanupCloudStorage() {
	Page<Blob> blobs = this.storage.list(this.cloudInputBucket);
	for (Blob blob : blobs.iterateAll()) {
		blob.delete();
	}
}
 
Example 18
Source File: GoogleCloudPropertiesFile.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 4 votes vote down vote up
public void remove() throws IOException {
  Blob blob = bucket.get(key);
  if (blob != null) {
    blob.delete();
  }
}