简体   繁体   English

无法删除,下载Blob并将其上载到Azure云存储

[英]Unable to delete, download and upload blob to Azure Cloud Storage

I've encountered a situation where I'm unable to perform certain operations such as deleting a blob inside a container in my storage account. 我遇到过一种情况,我无法执行某些操作,例如删除存储帐户中容器内的blob。 I've browsed these forums and tried various potential solutions, like giving the right permission or generating a SAS token. 我浏览了这些论坛并尝试了各种可能的解决方案,例如给予正确的权限或生成SAS令牌。 I was unable to get it working so I also contacted Azure Support but I wasn't able to get the help I needed. 我无法让它工作,所以我也联系了Azure支持,但我无法得到我需要的帮助。

I'm working on a .NET web application that, when a user is logged in, lists their stored blobs and gives them the possibility to download their files or upload new ones or even delete. 我正在开发一个.NET Web应用程序,当用户登录时,会列出他们存储的blob并让他们可以下载文件或上传新文件甚至删除。 I'm able to retreive and display the correct blobs without a problem. 我能够毫无问题地检索并显示正确的blob。 When I try to delete or download, I encounter this problem: "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." 当我尝试删除或下载时,我遇到了这个问题:“您要查找的资源已被删除,其名称已更改,或暂时不可用。”

Here's a snippet from my code 这是我代码中的一个片段

    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

I've made sure that the string values represent correct containers and blobs. 我已确保字符串值表示正确的容器和blob。

I also tried inserting some sort of MIME to my web.config file 我还尝试在我的web.config文件中插入某种MIME

<staticContent>
  <mimeMap fileExtension=".txt" mimeType="text/plain"/>
  <mimeMap fileExtension=".jpg" mimeType="image/jpeg"/>
</staticContent>

Thanks in advance. 提前致谢。

EDIT: Here's how the method looks in my controller 编辑:这是该方法在我的控制器中的样子

public ActionResult Delete(string name) {
    StorageModel.deleteBlob(name);
    return View(refreshList());
}//delete

And here is how it looks on my cshtml 这是它在我的cshtml上的样子

@Html.ActionLink("Delete", "Delete", new { id=item.name  })

Even if you are marking this as correct answer I'm telling you that you kind of messed up OOP. 即使你把这个标记为正确的答案,我告诉你,你有点搞砸OOP。

  • make sure you are buiding corectly in the constructor your storage account and your blob container name (init method of yours with username as parameter witch in fact is container name) 确保你在构造函数中核心地存储你的存储帐户和你的blob容器名称(你的init方法,用户名作为参数,其实就是容器名)
  • your init method is static, I'm 100% sure you are tryng to delete the file from an incorrect container, container name can be alternated from another StorageModel.init("some_container") 你的init方法是静态的,我100%肯定你是在尝试从不正确的容器中删除文件,容器名称可以从另一个StorageModel.init(“some_container”)中替换
  • so make sure you are in the correct container 所以要确保你在正确的容器中
  • before deleting or whatever are you doing check if your file exists 在删除之前或你正在做什么检查你的文件是否存在

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

call this thingy here: 在这里称这个东西:

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
}

But the biggest problem of yours is that static object StorageModel, even this is what you are looking for, and will fix the problem, this isn't the right way to do it 但是你的最大问题是静态对象StorageModel,即使这是你正在寻找的,并且会解决问题,这不是正确的方法

public ActionResult Delete(string name) {
    StorageModel.Init("Correct Container Name")
    StorageModel.deleteBlob(name);
    return View(refreshList());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM