【问题标题】:ASP.NET Core delete image blob from azure & from databaseASP.NET Core 从 azure 和数据库中删除图像 blob
【发布时间】:2018-11-21 04:38:30
【问题描述】:

我有一个 ASP.NET 应用程序,它可以成功地将图像上传到 azure 容器。我正在尝试创建一个删除 blob 中特定图像的函数,并从数据库中删除该图像。从数据库中删除它并不困难,但尝试从 azure 中删除它会引发异常。我在网上搜索了答案,但没有!什么都有帮助!下面是代码:

错误异常

An unhandled exception occurred while processing the request.
ArgumentNullException: Value cannot be null.
Parameter name: connectionString
Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(string connectionString)

InterfaceService.cs

 public CloudBlobContainer GetBlobContainer(string azureCString, string containerName)
    {
        var account = CloudStorageAccount.Parse(AzureCString);
        var blobClient = account.CreateCloudBlobClient();
        return blobClient.GetContainerReference(containerName);
    }

Products.cs

 public async Task<IActionResult> Delete(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var product = await _context.Product
            .SingleOrDefaultAsync(m => m.Id == id);
        if (product == null)
        {
            return NotFound();
        }

        return View(product);
    }


    [HttpPost, ActionName("Delete")]
    public async Task<IActionResult> DeleteConfirmed(int id, IFormFile file)
    {
        var product = await _context.Product.SingleOrDefaultAsync(m => m.Id == id);
        var container = _interfaceService.GetBlobContainer(AzureCString, "azureCstring");
        var content = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
        var fileName = content.FileName.Trim('"');
        var blob = container.GetBlockBlobReference(fileName);
        await blob.DeleteIfExistsAsync();

        _context.Product.Remove(product);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }

【问题讨论】:

  • 异常?需要详细说明吗?
  • 我添加了更多详细信息
  • 如果您使用秘密管理器,这可能与秘密管理器有关
  • 我希望是这样,但我没有使用秘密管理器。
  • 您是否已逐步执行 GetBlobContainer 方法并验证您的字符串是否存在?我的猜测是它被提供为 null。

标签: c# asp.net azure-blob-storage


【解决方案1】:

根据异常,您的连接字符串似乎为空。您可以在 GetBlobContainer 函数中使用带有硬代码的连接字符串并再次对其进行测试。

根据我的理解,您需要在 GetBlobContainer 函数中将代码 var account = CloudStorageAccount.Parse(AzureCString); 更改为 var account = CloudStorageAccount.Parse(azureCString);

public CloudBlobContainer GetBlobContainer(string azureCString, string containerName)
    {
        var account = CloudStorageAccount.Parse(azureCString); // change AzureCString to azureCString
        var blobClient = account.CreateCloudBlobClient();
        return blobClient.GetContainerReference(containerName);
    }

还要确保 DeleteConfirmed 函数中的AzureCString 不为空。正如我提到的,您可以使用硬代码连接字符串。它应该可以工作。

【讨论】:

  • 更新:所以我接受了您的建议,并着手对连接字符串进行硬编码。单步执行我的代码后,代码不再为空。但是,发生了一个新异常:“处理请求时发生未处理的异常。NullReferenceException:对象引用未设置为对象的实例。”它引用“var content = ContentDispositionHeaderValue.Parse(file.ContentDisposition);”有什么线索吗?
  • 根据你提到的错误,这意味着file为空,所以请在调试时检查文件对象。
  • 感谢汤姆的回答。您对文件为空是正确的。我仍然没有设法解决这个问题。还是谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-09-17
  • 2021-02-11
  • 2019-03-04
  • 2018-02-05
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
相关资源
最近更新 更多