我遇到过一种情况,我无法执行某些操作,例如删除存储帐户中容器内的blob。我浏览了这些论坛并尝试了各种可能的解决方案,例如给予正确的权限或生成SAS令牌。我无法让它工作,所以我也联系了Azure支持,但我无法得到我需要的帮助。
我正在开发一个.NET Web应用程序,当用户登录时,会列出他们存储的blob,并让他们可以下载文件或上传新文件甚至删除。我能够毫无问题地检索并显示正确的blob。当我尝试删除或下载时,我遇到了这个问题:“您要查找的资源已被删除,其名称已更改,或暂时不可用。”
以下是我的代码
的摘录 public static void init(string username){
// Retrieve storage account from connection string.
storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
container = blobClient.GetContainerReference(username);
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
BlobContainerPermissions permissions = new BlobContainerPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
// The new shared access policy provides read/write access to the container for 24 hours.
permissions.SharedAccessPolicies.Add("mypolicy", new SharedAccessBlobPolicy(){
// To ensure SAS is valid immediately, don’t set the start time.
// This way, you can avoid failures caused by small clock differences.
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Create | SharedAccessBlobPermissions.Add | SharedAccessBlobPermissions.Delete
});
// Set the new stored access policy on the container.
container.SetPermissions(permissions);
}//init
public static void deleteBlob(string name){
CloudBlockBlob blob = container.GetBlockBlobReference(name);
blob.Delete();
}//delete
我确保字符串值代表正确的容器和blob。
我还尝试在我的web.config文件中插入某种MIME
<staticContent>
<mimeMap fileExtension=".txt" mimeType="text/plain"/>
<mimeMap fileExtension=".jpg" mimeType="image/jpeg"/>
</staticContent>
提前致谢。
编辑:以下是该方法在我的控制器中的显示方式
public ActionResult Delete(string name) {
StorageModel.deleteBlob(name);
return View(refreshList());
}//delete
以下是我在cshtml上的表现
@Html.ActionLink("Delete", "Delete", new { id=item.name })
答案 0 :(得分:0)
即使您将此标记为正确答案,我也告诉您,您有点搞砸OOP。
删除之前或您正在做的任何事情检查您的文件是否存在
public static bool FileExists(string fileName, out int errorCode)
{
try
{
errorCode = ErrorCode.SUCCESS;
return container.ListBlobs(fileName, true).Any();
}
catch (Exception) //TODO: treat/log exception
{
errorCode = ErrorCode.ERROR_LOOKING_FOR_FILE;
}
return false;
}
在这里称之为:
public static void deleteBlob(string name){
//exist file
int errCode = 0;
if (FileExists(name, out errCode))
{
CloudBlockBlob blob = container.GetBlockBlobReference(name);
blob.Delete();
return;
}
//do something if error
}
但你的最大问题是静态对象StorageModel, 即使这是你正在寻找的,并将解决问题,这不是正确的方法 它
public ActionResult Delete(string name) {
StorageModel.Init("Correct Container Name")
StorageModel.deleteBlob(name);
return View(refreshList());
}