Windows Azure - Blob storage initialization - Authorization Error
Background:
I have an Azure application with one web role which is an ASP.NET application (C#), which uses a charting application to display the results of a calculation. The charting application needs an XML file as input. In order to access this XML file (referenced in the JavaScript), I use XDocument and related classes to manipulate the file, then save it, chart control is loaded on page refresh.
Error:
When trying to operate (GetPermissions, Create, Create if doesn't exist, etc.) on the container object, I get the following error:
Server failed to authenticate the request. Make sure the value of Authoriza开发者_如何学Ction header is formed correctly including the signature.
I also tried creating the container in advance using SpaceBlock, this didn't seem to have to change the outcome.
Code:
Here is the function I am calling on Page_Load. The error occurs on the line in bold (GetPermissions):
private void InitializeStorage()
{
if (storageInitialized)
{
return;
}
lock (gate)
{
if (storageInitialized)
{
return;
}
try
{
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
});
// read account configuration settings
var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
// create blob container for images
blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("xml");
// configure container for public access
**var permissions = container.GetPermissions();**
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
CloudBlob opcBlob = container.GetBlobReference("OptionPriceChart.xml");
opcBlob.DownloadToFile("opcLocal.xml");
}
catch (WebException)
{
throw new WebException("Storage services initialization failure. "
+ "Check your storage account configuration settings. If running locally, "
+ "ensure that the Development Storage service is running.");
}
storageInitialized = true;
}
}
I can't see anything in your code that you've provided that would cause the problem you're talking about. You will need to make sure that you've done a CreateIfNotExist
before calling the permissions otherwise you will get a The specified container does not exist
error (which is what I'm guessing you were doing before you encountered your current problem).
As the code seems fine it's likely to mean that it's something in your environment that's causing you grief, most likely the connection string. I've tried replicating your problem by messing around with the connection string and the only way I've been able to get the exact same error was to use an AccountName
with a valid AccountKey
from a different account. So my suggestion is to go back to the Azure portal, go to your storage service and copy the Primary Access Key into your cloud config.
精彩评论