通过 SDK 将块 Blob 上传到 Azure 存储 - 服务器无法对请求进行身份验证
Upload Block Blob to Azure Storage via SDK - Server failed to authenticate the request
从我的 ASP.Net 核心应用程序中,我尝试将块 blob 上传到 Azure 存储服务。
我以微软为例:
var accountName = "accountname";
var accountKey = "key";
//var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("myconnectionstring");
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(Container);
// Retrieve reference to a blob named "test".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test");
// Create or overwrite the "test" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"E:\Drone\N_01004619000008.jpg"))
{
await blockBlob.UploadFromStreamAsync(fileStream);
}
这会导致以下错误:
StorageException: Server failed to authenticate the request. Make sure
the value of Authorization header is formed correctly including the
signature.
使用 fiddler 我还可以看到这个额外的错误信息:
The MAC signature found in the HTTP request 'blankedOutKey' is not the
same as any computed signature. Server used following string to sign:
'PUT'
...而 'blankedOutKey' 不是我实际用于身份验证的密钥。不确定它来自哪里。
正如您在我的代码片段中看到的那样,我尝试使用我的 accountName 和 accountKey 以及 ConnectionString。
我做错了什么?
编辑:
我在这里试过这个:
https://github.com/Azure-Samples/storage-dotnet-sas-getting-started
使用完全相同的连接字符串,它在 github 的解决方案中工作,但是当我将完整代码接管到我的 ASP.Net Core 1.1 应用程序时,我再次遇到提到的问题。所以我仔细检查了差异。事实证明,来自 github 的项目使用 "Microsoft.WindowsAzure.Storage" 的 6.2.0 版本,而我的 ASP.Net 核心应用程序使用 8.1.3 版本。
此外,我在 fiddler 中仔细检查了来自两个解决方案的 HTTP 请求。
这是旧版 SDK 中有效的那个:
User-Agent: WA-Storage/6.2.0 (.NET CLR 4.0.30319.42000; Win32NT
6.2.9200.0) x-ms-version: 2015-04-05 x-ms-client-request-id: c1aea889-73f8-4b6b-9a1f-2f2f9e021846 x-ms-date: Sun, 04 Jun 2017
12:23:37 GMT Authorization: SharedKey
mystoragename:F5yhOOh2taUBfUE+3wii1cYb0D7L+jZGVfs1xTgTme0=
Host: mystoragename.blob.core.windows.net Content-Length: 0
通过较新的SDK提交请求失败:
Connection: Keep-Alive Authorization: SharedKey
mystoragenamee:IxtXuZFIcPjeKnqjktxvfcQMRLkfHM5SVN9zvQZmBJc=
User-Agent: Azure-Storage/8.1.3 (.NET Core) x-ms-client-request-id:
d890d2e0-07af-4695-af1f-8020c9476774 x-ms-version: 2016-05-31
x-ms-date: Sun, 04 Jun 2017 12:24:49 GMT x-ms-request-root-id:
428e8e6279f1ff9d-4237b4f7 x-ms-request-id:
|428e8e6279f1ff9d-4237b4f7.1. Request-Id:
|428e8e6279f1ff9d-4237b4f7.1. Content-Length: 0 Host:
mystoragename.blob.core.windows.net
The MAC signature found in the HTTP request 'blankedOutKey' is not the same as any computed signature. Server used following string to sign: 'PUT'
根据异常来看,构造授权似乎不正确。但是您使用的是 Azure 存储 SDK。其他代码似乎发生了异常。
请尝试创建一个项目,然后再试一次。我无法用你在 .net core Asp.net 项目中提到的代码重现它。它在我这边工作正常。
以下是我的详细步骤:
1.Create .net 核心项目。
2.Add Azure 存储 SDK
3.Create你提到代码的功能。
public async Task UploadBlobAsync()
{
const string myconnctionstring = "DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=yourkey;EndpointSuffix=core.windows.net";
//var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(myconnctionstring);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("test");
container.CreateIfNotExistsAsync().Wait();
// Retrieve reference to a blob named "test".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.jpg");
// Create or overwrite the "test" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"file path"))
{
await blockBlob.UploadFromStreamAsync(fileStream);
}
}
4.Call函数
public IActionResult About()
{
ViewData["Message"] = "upload blob.";
UploadBlobAsync().Wait();
return View();
}
5.Run项目并访问它的页面,它工作正常。
6.I 也用fiddler抓到它
感谢@Tom Sun - MSFT,我被送到了正确的方向。
我现在比较了我从头开始的新 ASP.Net 核心应用程序和我的实际解决方案之间的所有依赖关系。
问题是以下包:
Microsoft.ApplicationInsights.AspNetCore
如果我使用 2.1.0-beta,我会遇到错误。
切换到最后一个稳定版 2.0.0 解决了我的问题
从我的 ASP.Net 核心应用程序中,我尝试将块 blob 上传到 Azure 存储服务。 我以微软为例:
var accountName = "accountname";
var accountKey = "key";
//var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("myconnectionstring");
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(Container);
// Retrieve reference to a blob named "test".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test");
// Create or overwrite the "test" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"E:\Drone\N_01004619000008.jpg"))
{
await blockBlob.UploadFromStreamAsync(fileStream);
}
这会导致以下错误:
StorageException: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
使用 fiddler 我还可以看到这个额外的错误信息:
The MAC signature found in the HTTP request 'blankedOutKey' is not the same as any computed signature. Server used following string to sign: 'PUT'
...而 'blankedOutKey' 不是我实际用于身份验证的密钥。不确定它来自哪里。 正如您在我的代码片段中看到的那样,我尝试使用我的 accountName 和 accountKey 以及 ConnectionString。
我做错了什么?
编辑: 我在这里试过这个: https://github.com/Azure-Samples/storage-dotnet-sas-getting-started
使用完全相同的连接字符串,它在 github 的解决方案中工作,但是当我将完整代码接管到我的 ASP.Net Core 1.1 应用程序时,我再次遇到提到的问题。所以我仔细检查了差异。事实证明,来自 github 的项目使用 "Microsoft.WindowsAzure.Storage" 的 6.2.0 版本,而我的 ASP.Net 核心应用程序使用 8.1.3 版本。
此外,我在 fiddler 中仔细检查了来自两个解决方案的 HTTP 请求。 这是旧版 SDK 中有效的那个:
User-Agent: WA-Storage/6.2.0 (.NET CLR 4.0.30319.42000; Win32NT 6.2.9200.0) x-ms-version: 2015-04-05 x-ms-client-request-id: c1aea889-73f8-4b6b-9a1f-2f2f9e021846 x-ms-date: Sun, 04 Jun 2017 12:23:37 GMT Authorization: SharedKey mystoragename:F5yhOOh2taUBfUE+3wii1cYb0D7L+jZGVfs1xTgTme0= Host: mystoragename.blob.core.windows.net Content-Length: 0
通过较新的SDK提交请求失败:
Connection: Keep-Alive Authorization: SharedKey mystoragenamee:IxtXuZFIcPjeKnqjktxvfcQMRLkfHM5SVN9zvQZmBJc= User-Agent: Azure-Storage/8.1.3 (.NET Core) x-ms-client-request-id: d890d2e0-07af-4695-af1f-8020c9476774 x-ms-version: 2016-05-31 x-ms-date: Sun, 04 Jun 2017 12:24:49 GMT x-ms-request-root-id: 428e8e6279f1ff9d-4237b4f7 x-ms-request-id: |428e8e6279f1ff9d-4237b4f7.1. Request-Id: |428e8e6279f1ff9d-4237b4f7.1. Content-Length: 0 Host: mystoragename.blob.core.windows.net
The MAC signature found in the HTTP request 'blankedOutKey' is not the same as any computed signature. Server used following string to sign: 'PUT'
根据异常来看,构造授权似乎不正确。但是您使用的是 Azure 存储 SDK。其他代码似乎发生了异常。
请尝试创建一个项目,然后再试一次。我无法用你在 .net core Asp.net 项目中提到的代码重现它。它在我这边工作正常。
以下是我的详细步骤:
1.Create .net 核心项目。
2.Add Azure 存储 SDK
3.Create你提到代码的功能。
public async Task UploadBlobAsync()
{
const string myconnctionstring = "DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=yourkey;EndpointSuffix=core.windows.net";
//var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(myconnctionstring);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("test");
container.CreateIfNotExistsAsync().Wait();
// Retrieve reference to a blob named "test".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.jpg");
// Create or overwrite the "test" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"file path"))
{
await blockBlob.UploadFromStreamAsync(fileStream);
}
}
4.Call函数
public IActionResult About()
{
ViewData["Message"] = "upload blob.";
UploadBlobAsync().Wait();
return View();
}
5.Run项目并访问它的页面,它工作正常。
6.I 也用fiddler抓到它
感谢@Tom Sun - MSFT,我被送到了正确的方向。 我现在比较了我从头开始的新 ASP.Net 核心应用程序和我的实际解决方案之间的所有依赖关系。
问题是以下包:
Microsoft.ApplicationInsights.AspNetCore
如果我使用 2.1.0-beta,我会遇到错误。 切换到最后一个稳定版 2.0.0 解决了我的问题