c# - 指定的容器不存在(Azure)

标签 c# .net azure .net-core azure-functions

下面的代码有效。但是当我尝试从容器中删除 blob 时,我收到异常“指定的容器不存在”:

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

删除功能:

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

当我在 Visual Studio 中运行它时,它删除工作,但在 azure 中不起作用。很奇怪。

最佳答案

GetBlobReference 的参数应该是 blob 的名称,而不是 fileUrl。

以下代码是我自己使用的,它可以工作。

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

关于c# - 指定的容器不存在(Azure),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62949375/

相关文章:

azure - 如何在 Azure 移动应用程序中实现自定义身份验证

wcf - 如何在 BizTalk Server 2010 中配置 netMessagingBinding

c# - ASP.NET C# TagBuilder SetInnerText 在空白处呈现 %20

c# - Base-64 字符数组的长度无效

c# - 需要专家对 .net 应用程序最快通信的评论

c# - 如何确定 appconfig 包含特定 key

python - Azure Database for PostgreSQL 灵活服务器使用 Django 速度缓慢

c# - ASP.NET MVC4 重定向到登录页面

c# - 使用 Linq 和继承绑定(bind)到对象的通用方法

.net - 什么时候加载程序集?