getting 404 error when connecting to azure storage

2019-07-01 18:42发布

问题:

I am going through the tutorial for blobs with azure storage accounts found here Azure Storage Tutorial

I have created the storage account on azure and it says it is up. I have copied the key and account name to my config file. I have verified the endpoint address in my application matches the one on my azure storage account. but when I run the code I get a 404 error. And when I copy and past the endpoint address from my azure account into a browser I also get a 404 error

here I the code. actually copied from the tutorial I did add the correct account name and key to the config string but removed them for this post

    // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

        // Retrieve reference to a blob named "myblob".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

        // Create or overwrite the "myblob" blob with contents from a local file.
        using (var fileStream = System.IO.File.OpenRead(@"myfile"))
        {
            blockBlob.UploadFromStream(fileStream);
        }

and the connection string

<appSettings>
    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=myAccount;AccountKey=mykey" />
</appSettings>

Any ideas why? Do I need to do something more in Azure?

回答1:

The problem was the container was not created or had been deleted. I added

container.CreateIfNotExist();

and it worked fine. I would have expect a different error not a 404. but that fixed it.