使用CreateIfNotExists方法在Azure存储中创建容器404错误,但可以使用Create方法

时间:2016-12-12 17:20:46

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

如果我尝试使用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未找到错误

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失败?

谢谢

1 个答案:

答案 0 :(得分:0)

我今天尝试了我的原始代码(使用CreateIfNotExists),一切都运行良好。

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

我很抱歉打扰你这个问题并感谢你的帮助。