Andrew Yanchak
Andrew Yanchak

Reputation: 45

The specified container does not exist (Azure)

Code below works. But when i'm trying to delete blob from container i`m getting exception "The specified container does not exist":

public static async Task Run([BlobTrigger("files-for-uploading/{name}", Connection = "AzureWebJobsStorage")]
        Stream myBlob, string name, ILogger log)
{
    try
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

        if (CloudStorageAccount.TryParse(config["AzureWebJobsStorage"],
            out CloudStorageAccount storageAccount))
        {
            ClientHandler clientHandler = new ClientHandler(HttpClientFactory.Create());
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = 
            blobClient.GetContainerReference(config["ContainerForFileUploading"]);
            DeleteFilesFromBlob(container, name, log);
        }
    }
    catch (Exception e)
    {
        throw;
    }
}

Delete function:

private static void DeleteFilesFromBlob(CloudBlobContainer container, string blobName, ILogger log)
{
    CloudBlob file = container.GetBlobReference(blobName);
    file.DeleteIfExists();
}

And it delete works when i'm running it in Visual Studio, but does not work in azure. Very strange.

Upvotes: 1

Views: 5593

Answers (1)

Daniel Björk
Daniel Björk

Reputation: 2507

The parameter to GetBlobReference should be the name of the blob and not the fileUrl.

The following code Im using myself and it works.

    public async Task<IActionResult> delete(string blobName)
    {
        string blobstorageconnection = _configuration.GetValue<string>("blobstorage");
        string blobContainer = _configuration.GetValue<string>("blobcontainer");

        CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);
        CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

        CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(blobContainer);
        var blob = cloudBlobContainer.GetBlobReference(blobName);
        await blob.DeleteIfExistsAsync();



        return View();
    }

Upvotes: 1

Related Questions