【发布时间】:2020-02-14 14:45:47
【问题描述】:
我有一个带有 blob (/images/filename) 的 Azure 存储容器。文件名(uri)在创建时存储在数据库中,来自文件上传保存函数:
blob.UploadFromStream(filestream);
string uri = blob.Uri.AbsoluteUri;
return uri;
文件上传工作正常,当通过 SAS 密钥下载传递给客户端时也工作正常。
要删除图像我有一个从 MS 示例中获取的辅助函数:MS Github example 这是函数:
internal bool DeleteFile(string fileURI)
{
try
{
Uri uri = new Uri(fileURI);
string filename = Path.GetFileName(uri.LocalPath);
CloudBlockBlob fileblob = container.GetBlockBlobReference(filename);
fileblob.Delete();
bool res = fileblob.DeleteIfExists();
return res; //Ok
}
catch(Exception ex)
{
Console.WriteLine(ex);
return false;
}
}
这一切都在一个辅助类中,其开头如下:
public class AzureHelpers
{
private string connection;
private CloudStorageAccount storageAccount;
private CloudBlobClient blobClient;
private CloudBlobContainer container;
public AzureHelpers()
{
connection = CloudConfigurationManager.GetSetting("myproject_AzureStorageConnectionString");
storageAccount = CloudStorageAccount.Parse(connection);
blobClient = storageAccount.CreateCloudBlobClient();
container = blobClient.GetContainerReference(Resources.DataStoreRoot);
container.CreateIfNotExists();
}
....
我故意在 deleteIfExists 之前添加了 delete 以导致异常并证明我怀疑它没有找到文件/blob。
然而,当我单步执行代码时,CloudBlockBlob 肯定存在并且具有正确的 URI 等。
我想知道这是否可能是权限问题?还是我错过了什么?
【问题讨论】:
标签: c# azure azure-blob-storage