【问题标题】:404 error creating container in Azure Storage using CreateIfNotExists method but can with Create method使用 CreateIfNotExists 方法在 Azure 存储中创建容器时出现 404 错误,但可以使用 Create 方法
【发布时间】:2017-04-27 15:09:33
【问题描述】:

如果我尝试使用 CreateIfNotExists 方法在我的应用程序中针对 Azure 存储创建容器,我会收到 404 错误。但是,如果我将代码更改为使用 Create 方法,一切正常。

public void UploadImageAsync(string containerName, string blobName, byte[] blobData)
{
    CloudBlobContainer container = _client.GetContainerReference(containerName);

    container.CreateIfNotExists();
    container.SetPermissions(new BlobContainerPermissions()
    {
        PublicAccess = BlobContainerPublicAccessType.Blob
    });

    CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

    blob.UploadFromByteArray(blobData, 0, photo.Data.Length);
}

抛出 404 Not found 错误

public void UploadImageAsync(string containerName, string blobName, byte[] blobData)
{
    CloudBlobContainer container = _client.GetContainerReference(containerName);

    try
    {
        container.Create();
        container.SetPermissions(new BlobContainerPermissions()
        {
            PublicAccess = BlobContainerPublicAccessType.Blob
        });
    }catch (Exception webException) { }

    CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

    blob.UploadFromByteArray(blobData, 0, photo.Data.Length);
}

完美运行:

如果我将两个图像上传到同一个容器,我会捕获异常。我试着用 if (container.Exists()) 但它也会启动 404 错误。

知道发生了什么吗?为什么 Create 工作正常,但 CreateIfNotExists 失败?

谢谢

【问题讨论】:

  • 您能告诉我们您正在使用的存储客户端库的版本吗?还有哪个操作失败了……是 CreateIfNotExists() 还是 SetPermissions()。在您的第二个代码 sn-p 中,您只是在吃异常。请尝试删除 catch 块,看看代码是否也在那里失败。最后,如果您可以通过 Fiddler 之类的工具跟踪请求/响应,将会很有帮助。

标签: .net azure azure-storage azure-blob-storage


【解决方案1】:

我今天尝试了我的原始代码(使用 CreateIfNotExists),一切正常。

我不知道是昨天的 Azure 问题还是什么。我正在使用当前版本(7.2.1.0),根据 Azure 昨天注册没有问题。

对于这个问题给您带来的困扰,我深表歉意,并感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 2020-09-12
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多