我有一个使用Azure移动应用程序构建的Xamarin UWP应用程序,我正在尝试使用SAS令牌和.Net Azure存储客户端(8.4.0)将BLOB上传到Azure存储。我可以通过Easy API节点功能为我的容器生成SAS令牌,但是当我尝试使用它来上传文件时,我收到错误“服务器无法验证请求。请确保授权的值标题正确形成,包括签名。“
Easy API代码:适用于GetUploadToken
var azure = require('azure-storage');
function generateSasToken(container, blobName, permissions) {
var connString = "<conn string from azure storage acct>"; // removed for StackOverflow question
var blobService = azure.createBlobService(connString);
var currentDate = new Date();
var startDate = new Date(currentDate);
startDate.setMinutes(currentDate.getMinutes() - 5);
console.log("startDate: " + startDate);
console.log("current date: " + currentDate);
var expiryDate = new Date(currentDate);
expiryDate.setMinutes(currentDate.getMinutes() + 60);
console.log("expiryDate: " + expiryDate);
permissions = permissions || azure.BlobUtilities.SharedAccessPermissions.READ;
var sharedAccessPolicy = {
AccessPolicy: {
Permissions: permissions,
Start: startDate,
Expiry: expiryDate
}
};
var sasToken = blobService.generateSharedAccessSignature(container, blobName, sharedAccessPolicy);
console.log("SAS Token: " + sasToken);
return {
token: sasToken,
uri: blobService.getUrl(container, blobName, sasToken)
};
}
module.exports = {
"post": function (req, res, next) {
if (req.body.Container) {
console.log("Container: " + req.body.Container);
console.log("BlobName:" + req.body.BlobName);
console.log("Permissions:" + req.body.Permissions);
// The following values can be used for permissions:
// "a" (Add), "r" (Read), "w" (Write), "d" (Delete), "l" (List)
// Concatenate multiple permissions, such as "rwa" = Read, Write, Add
var token = generateSasToken(req.body.Container, req.body.BlobName, req.body.Permissions);
res.status(200).type('application/json').json(token);
} else {
res.status(400).type("text/plain").text("Specify a value for 'container");
}
}
};
简易API输出
Container: sketches
BlobName:DSCF3726.JPG
Permissions:rwa
startDate: Thu Oct 05 2017 14:39:36 GMT+0000 (Coordinated Universal Time)
current date: Thu Oct 05 2017 14:44:36 GMT+0000 (Coordinated Universal Time)
expiryDate: Thu Oct 05 2017 15:44:36 GMT+0000 (Coordinated Universal Time)
SAS Token: st=2017-10-05T14%3A39%3A36Z&se=2017-10-05T15%3A44%3A36Z&sp=rwa&sv=2017-04-17&sr=b&sig=...
上传代码
internal async Task<string> UploadAsync(string fileName, byte[] dataArray)
{
var sketchFile = new SketchFile
{
BlobName = fileName,
Container = "sketches",
Permissions = "rwa"
};
var response = await CurrentClient.InvokeApiAsync<SketchFile, GetSasTokenResponse>(
"GetUploadToken",
sketchFile);
try
{
var blob = new CloudBlockBlob(new Uri(response.Uri));
await blob.UploadFromByteArrayAsync(dataArray, 0, dataArray.Length);
}
catch (StorageException se)
{
System.Diagnostics.Debug.WriteLine($"Error uploading {fileName}: {se.RequestInformation.ExtendedErrorInformation.ErrorMessage}.");
return null;
}
var uri = response.Uri.Substring(0, response.Uri.IndexOf('?'));
return uri;
}
答案 0 :(得分:1)
根据您的代码,我可以在我这边重现这个问题。我认为它可能是由您指定的Permissions
引起的。正如Permissions for a blob所述Add
权限如下:
允许的操作:将块添加到追加blob。
您可以使用Azure Storage Explorer并为您的块blob生成sas令牌以缩小此问题。
根据我的测试,将权限更改为rw
,然后我就可以将文件成功上传到azure blob存储。