Search code examples
c#visual-studio-2010azureblobazure-storage

The specified container does not exist


Am stuck with this error The specified container does not exist.

let me explain,

CloudBlobClient blobStorage = GetBlobStorage("upload");
CloudBlockBlob blob = BlobPropertySetting(blobStorage, Guid.NewGuid().ToString().ToLower() + Path.GetExtension(file.FileName));
blob.UploadFromStream(file.InputStream);

public static CloudBlobClient GetBlobStorage(string cloudBlobContainserName)
{
    CloudBlobClient blobStorage;

    try
    {
        var storageAccount = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");
        blobStorage = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobStorage.GetContainerReference(cloudBlobContainserName); 

        container.CreateIfNotExist();

        var permissions = container.GetPermissions();
        permissions.PublicAccess = BlobContainerPublicAccessType.Container;

        container.SetPermissions(permissions);
    }
    catch (Exception ex)
    {
        Logger.LogError(Log4NetLogger.Category.Exception, "Error in : BlobHandler.GetBlobStorage :>> Exception message: " + ex.Message);
        throw;
    }

    return blobStorage;
}

public static CloudBlockBlob BlobPropertySetting(CloudBlobClient cloudBlobClientReferenceName, string blobContentName)
{
    return cloudBlobClientReferenceName.GetBlockBlobReference(blobContentName);
}

and my StorageConnectionString is

<Setting name="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=duw;AccountKey=bla bla" />

container 'upload' and the storage account 'duw' exist.

executing blob.UploadFromStream(file.InputStream); statement causes the error.

stack trace :

at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.get_Result() at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.ExecuteAndWait() at Microsoft.WindowsAzure.StorageClient.TaskImplHelper.ExecuteImpl(Func`1 impl) at Microsoft.WindowsAzure.StorageClient.CloudBlob.UploadFromStream(Stream source, BlobRequestOptions options) at Microsoft.WindowsAzure.StorageClient.CloudBlob.UploadFromStream(Stream source) at DAL.Handlers.BlobHandler.CreateAd(HttpPostedFileBase file, Advertisement model) in D:\DU Server\trunk\Du Server\DAL\Handlers\BlobHandler.cs:line 151

Inner exception:

{"The remote server returned an error: (404) Not Found."}

can any body help me to sort this out.


Solution

  • Short version

    Try the following code for BlobPropertySetting function:

     public static CloudBlockBlob BlobPropertySetting(CloudBlobClient cloudBlobClientReferenceName, string blobContentName)
        {
            CloudBlockBlob blob = cloudBlobClientReferenceName.GetBlockBlobReference("upload/" + blobContentName);
            return blob;
        }
    

    Now for the longer version :)

    The reason you're getting this error is because of the way you are constructing the CloudBlockBlob object in BlobPropertySetting method. When you use your code, it creates a blob object with the following URI: https://duv.blob.core.windows.net/blobContentName. If you notice, there's no container name there. Since there's no container name, storage client library assumes that you're trying to create a blob in $root blob container which is a special blob container. You can read more about it here: http://msdn.microsoft.com/en-us/library/windowsazure/hh488356.aspx. Since your storage account does not have this container, you get 404 - Resource Not Found error.