0

I'm trying to delete some blobs in an Azure Storage container using the Java Azure Storage Library 4.0.0, as explained here. Seems like this should be an easy thing to do, so I assume I'm doing something wrong, as the code below doesn't delete anything. There are 4 blobs in the container.

String connectionString = String.format(
        "DefaultEndpointsProtocol=https;" +
        "AccountName=%s;" +
        "AccountKey=%s", accountName, accountKey);
CloudStorageAccount account =
        CloudStorageAccount.parse(connectionString);
CloudBlobClient client = account.createCloudBlobClient();
CloudBlobContainer container =
        client.getContainerReference("myContainer");

// This loop iterates 4 times, as expected
for (ListBlobItem item : container.listBlobs("prefix/", true)) {
    CloudBlockBlob blob = container.
            getBlockBlobReference(item.getUri().toString());
    if (blob.deleteIfExists()) {
        // never hits
    }
}

No exceptions are thrown, but the blobs remain. When I call delete() instead of deleteIfExists(), I get a StorageException: "the specified blob does not exist."

1 Answer 1

0

If you take a look at the API docs for getBlockBlobReference you'll see it takes the name (hence a string, and not a URI) of the blob. So, what you're doing here is trying to delete blobs whose name is the full URI of your blob. These of course don't exist.

What you want to do is simply check the type of the item and cast it to a blob. You can then do what ever operations you want.

      if (item instanceof CloudBlob) {
            blob = (CloudBlob) item;
      }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.