// Azure SDK client builders accept the credential as a parameter
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("https://<storage-account-name>.blob.core.windows.net/")
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
// If needed, you can create a BlobContainerClient object from the BlobServiceClient
BlobContainerClient containerClient = blobServiceClient
.getBlobContainerClient("<container-name>");
// If needed, you can create a BlobClient object from the BlobContainerClient
BlobClient blobClient = containerClient
.getBlobClient("<blob-name>");
public void deleteBlobWithSnapshots(BlobClient blobClient) {
Response<Boolean> response = blobClient.deleteIfExistsWithResponse(DeleteSnapshotsOptionType.INCLUDE, null,
null,
new Context("key", "value"));
if (response.getStatusCode() == 404) {
System.out.println("Blob does not exist");
} else {
System.out.printf("Delete blob completed with status %d%n", response.getStatusCode());
public void restoreBlobVersion(BlobContainerClient containerClient, BlobClient blobClient){
// List blobs in this container that match the prefix
// Include versions in the listing
ListBlobsOptions options = new ListBlobsOptions()
.setPrefix(blobClient.getBlobName())
.setDetails(new BlobListDetails()
.setRetrieveVersions(true));
Iterator<BlobItem> blobItem = containerClient.listBlobs(options, null).iterator();
List<String> blobVersions = new ArrayList<>();
while (blobItem.hasNext()) {
blobVersions.add(blobItem.next().getVersionId());
// Sort the list of blob versions and get the most recent version ID
Collections.sort(blobVersions, Collections.reverseOrder());
String latestVersion = blobVersions.get(0);
// Get a client object with the name of the deleted blob and the specified version
BlobClient blob = containerClient.getBlobVersionClient("sampleBlob.txt", latestVersion);
// Restore the most recent version by copying it to the base blob
blobClient.copyFromUrl(blob.getBlobUrl());