从我的ASP.Net Core应用程序中,我尝试将块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:服务器无法验证请求。确保 正确形成Authorization标头的值,包括 签名。
使用fiddler我还可以看到这个额外的错误信息:
在HTTP请求中找到的MAC签名' blankedOutKey'不是 与任何计算签名相同。服务器使用以下字符串进行签名 ' PUT'
...而“< blankedOutKey'不是我实际用于身份验证的密钥。不知道它来自哪里。 正如您在我的代码片段中看到的那样,我尝试使用我的accountName和accountKey以及ConnectionString。
我做错了什么?
修改 我试过这个: https://github.com/Azure-Samples/storage-dotnet-sas-getting-started
使用完全相同的连接字符串,它在github的解决方案中工作,但当我接管完整代码到我的ASP.Net Core 1.1应用程序时,我再次遇到了提到的问题。所以我仔细检查了差异。事实证明,来自github的项目使用了6.2.0版本的" Microsoft.WindowsAzure.Storage"而我的ASP.Net Core应用程序使用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:2017年6月4日星期日 12:23:37 GMT授权:SharedKey mystoragename:F5yhOOh2taUBfUE + 3wii1cYb0D7L + jZGVfs1xTgTme0 = 主持人:mystoragename.blob.core.windows.net内容长度:0
通过较新的SDK提交的请求失败:
连接:Keep-Alive授权: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。请求ID: | 428e8e6279f1ff9d-4237b4f7.1。内容长度:0主持人: mystoragename.blob.core.windows.net
答案 0 :(得分:1)
HTTP请求'blankedOutKey'中找到的MAC签名与任何计算签名不同。服务器使用以下字符串进行签名:'PUT'
根据例外情况,似乎构造授权不正确。但您使用的是Azure存储SDK。似乎异常发生在其他代码中。
请尝试创建项目并再次尝试。我不能用你在.net核心Asp.net项目中提到的代码来重复它。它在我身边正常工作。
以下是我的详细步骤:
1.创建一个.net核心项目。
2.添加Azure存储SDK
3.用你提到的代码创建函数。
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.拨打电话
public IActionResult About()
{
ViewData["Message"] = "upload blob.";
UploadBlobAsync().Wait();
return View();
}
5.运行项目并访问页面,它可以正常工作。
6.我也是小提琴手。
答案 1 :(得分:0)
感谢@Tom Sun - MSFT我已被送往正确的方向。 我现在比较了我从头开始的新ASP.Net Core应用程序与我的实际解决方案之间的所有依赖关系。
问题是以下方案:
Microsoft.ApplicationInsights.AspNetCore
如果我使用2.1.0-beta,我就遇到了错误。 切换到最后一个稳定的2.0.0解决了我的问题