c# - Windows Azure - Blob 存储初始化 - 授权错误

标签 c# authentication azure azure-blob-storage

背景:

我有一个带有一个 Web 角色的 Azure 应用程序,该角色是一个 ASP.NET 应用程序 (C#),它使用图表应用程序来显示计算结果。图表应用程序需要 XML 文件作为输入。为了访问这个 XML 文件(在 JavaScript 中引用),我使用 XDocument 和相关类来操作该文件,然后保存它,图表控件在页面刷新时加载。

错误:

当尝试在容器对象上进行操作(GetPermissions、Create、Create if does not exist等)时,出现以下错误:

服务器无法验证请求。确保授权 header 的值格式正确,包括签名。

我还尝试使用 SpaceBlock 提前创建容器,这似乎并没有改变结果。

代码:

这是我在 Page_Load 上调用的函数。错误发生在粗体行(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;
        }
    }

最佳答案

我在您提供的代码中看不到任何会导致您所讨论的问题的内容。您需要确保您已完成 CreateIfNotExist在调用权限之前,否则您将得到 The specified container does not exist错误(我猜您在遇到当前问题之前正在做的事情)。

由于代码看起来不错,这可能意味着您的环境中的某些东西导致了您的悲伤,很可能是连接字符串。我尝试通过弄乱连接字符串来复制您的问题,而我能够得到完全相同的错误的唯一方法是使用 AccountName具有有效的 AccountKey来自不同的帐户。因此,我的建议是返回 Azure 门户,转到您的存储服务并将主访问 key 复制到您的云配置中。

关于c# - Windows Azure - Blob 存储初始化 - 授权错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4137932/

相关文章:

c# - WCF 客户端错误处理

authentication - 如何在布局中访问登录的用户数据。 (蛋糕PHP 3)

c# - 唤醒 Azure 函数

c - 将程序设置为我的登录 shell

azure - 如何使用 Azure Functions 检查授权

azure - U-SQL/ADLA : How to get multiple indexes and partitions on table?

azure - 管道从 azure 工件恢复 nuget 包

c# - Zlib 兼容的压缩流?

c# - Websockets 将 byte[] 转换为字符串

c# - 检查字符是否是字母