The specified container does not exist
Asked Answered
E

3

17

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.

Execrative answered 18/9, 2013 at 4:43 Comment(4)
first thing to do is to check if container is being created properly using any of azure storage viewers.like cloudstorageexplorer. secondly, see if you are getting reference of blob properly in BlobPropertySetting method?Inspirational
blobStorage.GetContainerReference("upload") gives the following {Microsoft.WindowsAzure.StorageClient.CloudBlobContainer} Attributes: {Microsoft.WindowsAzure.StorageClient.BlobContainerAttributes} Metadata: {System.Collections.Specialized.NameValueCollection} Name: "upload" Properties: {Microsoft.WindowsAzure.StorageClient.BlobContainerProperties} ServiceClient: {Microsoft.WindowsAzure.StorageClient.CloudBlobClient} Uri: {duw.blob.core.windows.net/upload}Execrative
I'm not sure i understand those first 3 lines.. are they class level vars or are you running those from another method not shown here?Titanium
@paqogomez: class level vars.. all methods i used are shown here..Execrative
L
9

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.

Lurid answered 18/9, 2013 at 5:44 Comment(2)
Thanks a lot for spending your valuable time.. but am confused with the flow of code CloudBlobContainer container = blobStorage.GetContainerReference(cloudBlobContainserName); container.CreateIfNotExist(); these two lines are executed na..?Execrative
Yes, they are executed. You should see a blob container by the name "upload" in your storage account. Essentially you lost the reference to your container when you left that method. Yet another alternative would be to return an object of type CloudBlobContainer from this method instead of CloudBlobClient. Then in your BlobPropertySetting when you pass this blob container object, you don't have to prepend the name of the container in GetBlockBlobReference. HTH.Lurid
D
4

I am very late, but still thought if my answer would be useful for anyone.

I resolved this error by putting the correct "container name". It was different by default. I have cloned this GIT project : https://github.com/Azure-Samples/storage-blob-upload-from-webapp-node

const
      express = require('express')
    , router = express.Router()
    , azureStorage = require('azure-storage')
    , blobService = azureStorage.createBlobService()
    , containerName = 'container' // added container name here, as my container name
    , config = require('../config')
;
Devilment answered 26/10, 2018 at 9:16 Comment(1)
I was making the same mistake, even I forgot to change the container name. Now my service is starting up, but when trying to upload a file it is throwing 500 error in the upload.js file. TypeError: Cannot read property 'status' of undefined. Not sure what mistake I am making here.Swirly
S
0

Ensure you are also setting the blob name correctly. An upload attempt to a valid container with a missing blob name will also return "404 Not Found".

Salmon answered 6/5, 2023 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.