我正在尝试为 Salesforce Apex 中的 Azure 文件存储创建共享访问签名 (SAS),但没有成功。这是我的代码:
String link = 'https://<accountName>.file.core.windows.net/<folderName>/test.txt';
String key = '...' // key of the account
String spe = 'r'; //read
String st = '2018-06-07T08:49:00Z';
String se = '2018-06-09T08:49:00Z';
String res = '/file/<accountName>/<folderName>/test.txt';
String sv = '2017-11-09';
String sr = 'f'; // file
String spr = 'https';
String sts = spe + '\n' +
st + '\n' +
se + '\n' +
res + '\n' +
spr + '\n'+
sv + '\n';
Blob data = crypto.generateMac('HmacSHA256', Blob.valueOf(sts), Blob.valueOf(key));
String sas = EncodingUtil.base64Encode(data);
sas = EncodingUtil.urlEncode(sas, 'UTF-8');
link += '?sv=' + sv + '&st=' + st + '&se=' + se + '&sr=' + sr + '&sp=' + spe
+ '&spr=' + spr + '&sig=' + sas;
System.debug(link);
当我在浏览器中复制粘贴链接时,响应是:
<Error>
<Code>AuthenticationFailed</Code>
<Message>
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:... Time:2018-06-07T21:56:01.7374549Z
</Message>
<AuthenticationErrorDetail>
Signature did not match. String to sign used was r 2018-06-07T08:49:00Z 2018-06-09T08:49:00Z /file/<accountName>/<folderName>/test.txt https 2017-11-09
</AuthenticationErrorDetail>
</Error>
我在这里做错了什么?
最佳答案
正如 @superfell 在评论中提到的,问题出在以下代码行:
Blob data = crypto.generateMac('HmacSHA256', Blob.valueOf(sts), Blob.valueOf(key));
请将这行代码更改为:
Blob data = crypto.generateMac('HmacSHA256', Blob.valueOf(sts), EncodingUtil.base64Decode(key));
并且您不应该收到此错误。本质上, key 是一个 Base64 编码的字符串,您需要使用正确的解码方法来获取字节数组。
关于azure - 在 Salesforce 中创建 Azure 文件存储 SAS 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50750841/