简体   繁体   中英

Java Azure Storage Client - deleting blobs

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."

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;
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM