Azure 存储:无法使用定义的凭据访问容器 blob

Azure Storage : Unable to access Container blobs with defined Credentials

我们正在为存储帐户设置一个密钥,然后使用它来访问以下内容;

var storageCredentials = new StorageCredentials(mediaStorageAccountName, base64EncodedKey);
var storageAccount = new CloudStorageAccount(storageCredentials, true);
var connString = storageAccount.ToString(true);

然后,使用相同的“storageAccount”创建 Blob 客户端;

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

并获取容器;

var container = blobClient.GetContainerReference(ContainerName);

“storageAccount”凭证属性为“IsSAS”FALSE,“IsSharedKey”为 TRUE,“IsToken”为 FALSE,“KeyName”为 NULL。

但是,当使用 OpenReadAsync 访问 Blob 时,它失败并出现以下异常;

The remote server returned an error: (403) Forbidden.,The remote server returned an error: (403) Forbidden. Line number: Microsoft.WindowsAzure.Storage Trace: at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndExecuteAsync[T](IAsyncResult result) at Microsoft.WindowsAzure.Storage.Blob.CloudBlob.EndExists(IAsyncResult asyncResult) at Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions.<>c__DisplayClass2`1.b__0(IAsyncResult ar)

它基本上正确地获取了对 Container/Blobs 等的所有引用(给出了正确的名称),但是当它尝试 read/download/upload 那些时,它失败了。

此外,不是直接使用“storageAccount”引用,即使它是通过以下方式保护的,它也会给出相同的异常;

CloudStorageAccount storageAccount = new CloudStorageAccount(
   new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, base64EncodedKey), true);

这里有什么问题以及如何解决这个问题? 为什么 KeyName 为 NULL?这是导致此问题的原因吗?

使用了错误的access key经常导致的403 forbidden异常

当您使用 Authorize with Shared Key 时,所有授权请求都必须包含请求的协调世界时 (UTC) 时间戳。您可以在 x-ms-date header 或标准 HTTP/HTTPS Date header.

中指定时间戳

存储服务确保请求到达服务时不早于 15 分钟。这可以防止某些安全攻击,包括重放攻击。当此检查失败时,服务器 returns 响应代码 403(禁止访问)。

因此,检查您的服务器数据时间。