azure - 在 Salesforce 中创建 Azure 文件存储 SAS 不起作用

标签 azure salesforce azure-storage apex

我正在尝试为 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/

相关文章:

azure - 如何在 Azure 门户中为 Azure Blob 容器创建具有存储访问策略的共享访问签名?

sql-server - SQL Azure 和 Azure 表存储如何比较?

c# - 在 WPF 应用程序中加载大量 Azure Blob 数据

salesforce - Apex 错误 - 尝试访问记录值时变量不存在

css - 如何更改顶点 :pageBlock in Salesforce 的边框顶部颜色

azure - 在 ARM 模板中应用 Azure Key Vault 无 self 权限属性

Azure 标准 Web 应用程序计划,有几个网站?

methods - 何时在 Salesforce 中将方法声明为静态

.net - 无法在 WinDGB 中打开 Azure 应用服务 .NET Core 2 转储文件(转储文件中存在 2 个运行时)

python - 从 ADLS2 传输到计算目标的速度非常慢 Azure 机器学习