【发布时间】:2013-09-01 06:16:38
【问题描述】:
当我尝试使用使用天蓝色客户端存储库生成的 SAS 密钥时,我不断收到来自 Azure 存储服务的 403 身份验证错误。调用代码正在抛出异常。
这是我得到的例外:
StatusMessage:Server failed to authenticate the request. Make sure the value of
Authorization header is formed correctly including the signature.
ErrorCode:AuthenticationFailed
这是正在生成的 SAS 密钥(最后的签名不准确,因为我不想分享):
https://evinsight.blob.core.windows.net/jimsworld/jellyfish2.png?sv=2012-02-12&se=2013-08-29T03%3A36%3A52Z&sr=b&sp=w&sig=FJALKJFLKASJDF%JLKSDJLK%LJDLFKSDFJKL
这里是调用代码:
/// Create the document and file objects and then return the fileAccessToken
/// <param name = "file_name"></param> */
public void GetSasInfoFromEb(string file_name)
{
/*********************************** EVINSIGHT TESTING CODE ********************************************/
try
{
AzureStorageTester ast = new AzureStorageTester();
BlobSasUri = ast.getStorageLibrarySas(file_name);
}
catch (Exception ex)
{
throw ex;
}
/***************************************************************************************************************/
}
/// Sends the whole small file to Azure Blob
public void WriteSmallFileToBlob()
{
int fileId = 0;
try
{
Blob = new CloudBlockBlob(new Uri(BlobSasUri));
Blob.UploadFromStream(File.InputStream);
File.InputStream.Close();
}
catch (Exception ex)
{
throw ex;
}
}
这是 SAS 生成代码:
/// <summary>
/// Returns the URI similar to how eB does it
/// </summary>
/// <param name="blobName">The name of the file being uploaded/downloaded</param>
/// <returns>The full uri for blob access</returns>
public string getStorageLibrarySas(string blobName)
{
string sasKey;
string uri;
setupBlobContainer();
blobName = blobName.ToLower();
_blob = _cloudRootContainer.GetBlockBlobReference(blobName);
sasKey = _blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Write,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1)
});
return string.Format(CultureInfo.InvariantCulture, "{0}{1}", _blob.Uri, sasKey);
}
/// Creates the Blob Container
private void setupBlobContainer()
{
try
{
Microsoft.WindowsAzure.Storage.Auth.StorageCredentials credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(_accountName, _accountKey);
// Create the storage account with the connection string
Microsoft.WindowsAzure.Storage.CloudStorageAccount _cloudStorageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(credentials, true);
_cloudBlobClient = _cloudStorageAccount.CreateCloudBlobClient();
_cloudRootContainer = _cloudBlobClient.GetContainerReference(_rootPath.ToLower());
_cloudRootContainer.CreateIfNotExists();
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;
_cloudRootContainer.SetPermissions(containerPermissions);
}
catch (Exception ex)
{
throw ex;
}
}
有什么想法吗?欣赏!
【问题讨论】:
-
你从哪里得到异常?在上面的代码中,还是在尝试使用您的 SAS URI 之一的调用应用程序中?如果是后者,我可以很好地猜测它与时钟漂移有关。如果是前者,请指出异常发生的位置。
-
尝试使用 SAS 的调用代码抛出异常。服务器基本上拒绝 SAS 密钥。我漏掉了开始时间,那不应该解决时钟偏差问题吗?
-
你能通过 Fiddler 追踪请求/响应吗?您应该通过这种方式获得有关 403 错误的更多信息。
标签: .net azure key storage blob