Java Azure存储客户端 - 删除blob(Java Azure Storage Client - deleting blobs)

我正在尝试使用Java Azure存储库4.0.0删除Azure存储容器中的某些blob,如此处所述。 看起来这应该是一件容易的事,所以我假设我做错了,因为下面的代码不会删除任何东西。 容器中有4个blob。

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

没有异常被抛出,但是blob仍然存在。 当我调用delete()而不是deleteIfExists() ,我得到一个StorageException:“指定的blob不存在。”

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

最满意答案

如果您查看getBlockBlobReference的API文档,您会看到它获取blob的名称(因此是字符串,而不是URI)。 所以,你在这里做的是尝试删除名称是你的blob的完整URI的blob。 这些当然不存在。

你想要做的只是检查项目的类型并将其投射到blob。 然后,您可以执行您想要的任何操作。

if (item instanceof CloudBlob) { blob = (CloudBlob) item; }

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; }Java Azure存储客户端 - 删除blob(Java Azure Storage Client - deleting blobs)

我正在尝试使用Java Azure存储库4.0.0删除Azure存储容器中的某些blob,如此处所述。 看起来这应该是一件容易的事,所以我假设我做错了,因为下面的代码不会删除任何东西。 容器中有4个blob。

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

没有异常被抛出,但是blob仍然存在。 当我调用delete()而不是deleteIfExists() ,我得到一个StorageException:“指定的blob不存在。”

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

最满意答案

如果您查看getBlockBlobReference的API文档,您会看到它获取blob的名称(因此是字符串,而不是URI)。 所以,你在这里做的是尝试删除名称是你的blob的完整URI的blob。 这些当然不存在。

你想要做的只是检查项目的类型并将其投射到blob。 然后,您可以执行您想要的任何操作。

if (item instanceof CloudBlob) { blob = (CloudBlob) item; }

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