Skip to content

Commit

Permalink
docs: Update DeleteObject Sample to be clearer on object versioning b…
Browse files Browse the repository at this point in the history
…ehavior (#2595)

* docs: Update DeleteObject Sample to be clearer on object versioning behavior

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Update samples/snippets/src/main/java/com/example/storage/object/DeleteObject.java

Co-authored-by: BenWhitehead <[email protected]>

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* adding in imports

* rephrase comments

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: BenWhitehead <[email protected]>
  • Loading branch information
3 people authored Jun 26, 2024
1 parent 60692c2 commit 79b7cf0
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

// [START storage_delete_file]
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

Expand All @@ -38,16 +39,19 @@ public static void deleteObject(String projectId, String bucketName, String obje
System.out.println("The object " + objectName + " wasn't found in " + bucketName);
return;
}

// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload returns a 412 error if
// the object's generation number does not match your precondition.
Storage.BlobSourceOption precondition =
Storage.BlobSourceOption.generationMatch(blob.getGeneration());

storage.delete(bucketName, objectName, precondition);

System.out.println("Object " + objectName + " was deleted from " + bucketName);
BlobId idWithGeneration = blob.getBlobId();
// Deletes the blob specified by its id. When the generation is present and non-null it will be
// specified in the request.
// If versioning is enabled on the bucket and the generation is present in the delete request,
// only the version of the object with the matching generation will be deleted.
// If instead you want to delete the current version, the generation should be dropped by
// performing the following.
// BlobId idWithoutGeneration =
// BlobId.of(idWithGeneration.getBucket(), idWithGeneration.getName());
// storage.delete(idWithoutGeneration);
storage.delete(idWithGeneration);

System.out.println("Object " + objectName + " was permanently deleted from " + bucketName);
}
}
// [END storage_delete_file]

0 comments on commit 79b7cf0

Please sign in to comment.