Reputation: 13
I'm trying to delete a blob from a private blob container using the deleteBlobIfExists()
function from the azure-storage
npm package. But, when the function is executed, the result
always comes back as false
, which means the blob "doesn't exist". But the blob does exist. I'm just assuming that it can't find the blob because the container's access is set to "private". Help?
const blobService = azure.createBlobService();
blobService.deleteBlobIfExists("my-blob-container", "my-blob", (err, result) => {
if(err) {
console.log(err);
}
});
Upvotes: 1
Views: 2623
Reputation: 416
Sounds like you have a race condition with Azure if you're calling this shortly after a blob creation event.
Upvotes: 0
Reputation: 23111
If your container's access level is private, you need to provide storage connection string or account name and account key when you use azure.createBlobService()
to create blob client.
Besides, The sdk azure-storage
is Azure Storage nodejs V2. It is a legacy SDK. I suggest you use the sdk @azure/storage-blob
. It is the latest SDK. Regarding how to use it, please refer to the following steps
az ad sp create-for-rbac -n <your-application-name> --skip-assignment
az keyvault set-policy --name <your-key-vault-name> --spn $AZURE_CLIENT_ID --secret-permissions backup delete get list purge recover restore set
AZURE_TENANT_ID=<tenant id>
AZURE_CLIENT_ID=<app id>
AZURE_CLIENT_SECRET=<password>
npm install @azure/identity
npm install @azure/storage-blob
var storage = require("@azure/storage-blob")
const { DefaultAzureCredential } = require("@azure/identity");
const defaultAzureCredential = new DefaultAzureCredential();
const blobclient = new storage.BlobServiceClient("<blob url>",defaultAzureCredential)
if(blobClient.exists()){
blobClient.delete()
}
Upvotes: 1
Reputation: 187
I confirm with what Jim Xu is saying - add your connection string when calling
azure.createBlobService()
and it deletes the blob successfully. I tried with the quickstart sample code and was able to successfully delete a blob in a private container.
const CONNECT_STR = process.env.CONNECT_STR;
console.log('\nDeleting blob...');
// Delete blob
const blobService = azure.createBlobService(CONNECT_STR);
blobService.deleteBlobIfExists(containerName, blobName, (err, result) => {
if(err) {
console.log(err);
}
});
console.log("Blob was deleted successfully.");
Upvotes: 0