Azure / azure-storage-java

Microsoft Azure Storage Library for Java
https://docs.microsoft.com/en-us/java/api/overview/azure/storage
MIT License
189 stars 163 forks source link

unable to delete block blob using CloudBlockBlob.delete() #500

Closed deekshitha-kallem closed 4 years ago

deekshitha-kallem commented 4 years ago

Which service(blob, file, queue, table) does this issue concern?

Block Blob

Which version of the SDK was used?

V8.0.0 (maven repo)

What problem was encountered?

I am trying to access storage block blobs created when we enable NSG Flow logs in Azure. I am able to access the blobs and their content through sdk. But when I try to delete them, I get the error that "the specific block doesn't exists". I am able to delete the blob manually in Azure portal. I face problem with doing it through sdk

The code I used is this:

String connectionString = null; 
ApplicationTokenCredentials creds = new ApplicationTokenCredentials(OAUTH_CLIENT_KEY,OAUTH_ACCESSTOKEN_URL,OAUTH_CLIENT_SECRET, ENVIRONMENT);
azure = Azure.authenticate(creds).withSubscription(subscriptionId);
StorageAccount storageAccount = azure.storageAccounts().getByResourceGroup(resourceGroupName, storageAccountName);

storageAccountAccessKey = storageAccount.getKeys().get(0).value();
connectionString = "DefaultEndpointsProtocol=https;AccountName=" + storageAccountName + ";AccountKey=" + storageAccountAccessKey + ";EndpointSuffix=core.windows.net;";

CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
CloudBlobClient blobClient = account.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference("insights-logs-networksecuritygroupflowevent");

for(ListBlobItem blob : container.listBlobs(PATH_TO_THE_BLOB)) {
    CloudBlockBlob block = container.getBlockBlobReference(blob.getUri().toString());
    System.out.println(block.getName());
    try {
        block.delete();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

======================================== I get the following exception:

com.microsoft.azure.storage.StorageException: The specified blob does not exist. at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:87) at com.microsoft.azure.storage.core.StorageRequest.materializeException(StorageRequest.java:313) at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:185) at com.microsoft.azure.storage.blob.CloudBlob.delete(CloudBlob.java:999) at com.microsoft.azure.storage.blob.CloudBlob.delete(CloudBlob.java:955)

===========================================

Note: The credentials I use has "contributor" role.

Have you found a mitigation/solution?

Nope.

jaschrep-msft commented 4 years ago

Hi @deekshitha-kallem, thanks for reaching out to us. I'm looking at the for loop you posted:

for(ListBlobItem blob : container.listBlobs(PATH_TO_THE_BLOB)) {
    CloudBlockBlob block = container.getBlockBlobReference(blob.getUri().toString());
    System.out.println(block.getName());
    try {
        block.delete();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

Can you give me an example of what System.out.println(block.getName()); is printing, as well as what System.out.println(block.getUri()); would print?

deekshitha-kallem commented 4 years ago

Hi @jaschrep-msft , my PATH_TO_THE_BLOB variable has a value something like this "resourceId=/SUBSCRIPTIONS/{subscription-id}/RESOURCEGROUPS/{resource-group-name}/PROVIDERS/MICROSOFT.NETWORK/NETWORKSECURITYGROUPS/{network-security-group-name}/y=2019/m=09/d=06/h=07/m=00/macAddress=000D3A3F6CB8/"

System.out.println(blob.getUri()); gives https://{storageaccountname}.blob.core.windows.net/insights-logs-networksecuritygroupflowevent/resourceId=/SUBSCRIPTIONS/{subscription-id}/RESOURCEGROUPS/{resource-group-name}/PROVIDERS/MICROSOFT.NETWORK/NETWORKSECURITYGROUPS/{network-security-group-name}/y=2019/m=09/d=06/h=07/m=00/macAddress=000D3A3F6CB8/PT1H.json

System.out.println(block.getName()); gives the same above result.

System.out.println(block.getUri()); gives https://{storageaccountname}.blob.core.windows.net/insights-logs-networksecuritygroupflowevent/https://{storageaccountname}.blob.core.windows.net/insights-logs-networksecuritygroupflowevent/resourceId=/SUBSCRIPTIONS/{subscription-id}/RESOURCEGROUPS/{resource-group-name}/PROVIDERS/MICROSOFT.NETWORK/NETWORKSECURITYGROUPS/{network-security-group-name}/y=2019/m=09/d=06/h=07/m=00/macAddress=000D3A3F6CB8/PT1H.json

I am sorry. I should have given only the path instead of the whole uri. I understood my mistake here. Once I change the uri to only resourceId=/SUBSCRIPTIONS/{subscription-id}/RESOURCEGROUPS/{resource-group-name}/PROVIDERS/MICROSOFT.NETWORK/NETWORKSECURITYGROUPS/{network-security-group-name}/y=2019/m=09/d=06/h=07/m=00/macAddress=000D3A3F6CB8/PT1H.json, am able to delete the blob.

To make it clear, in the above URI, I got two https in the path. Now that I've removed it, I am able to delete the blob.

https://{storageaccountname}.blob.core.windows.net/insights-logs-networksecuritygroupflowevent/https://{storageaccountname}.blob.core.windows.net/insights-logs-networksecuritygroupflowevent/resourceId=/SUBSCRIPTIONS/{subscription-id}/RESOURCEGROUPS/{resource-group-name}/PROVIDERS/MICROSOFT.NETWORK/NETWORKSECURITYGROUPS/{network-security-group-name}/y=2019/m=09/d=06/h=07/m=00/macAddress=000D3A3F6CB8/PT1H.json