在Google脚本中生成AWS S3的签名

时间:2018-09-19 00:05:29

标签: amazon-web-services api authentication amazon-s3 google-apps-script

我正在尝试在Google脚本中生成签名,但运气不佳。

我的代码如下:

function getAwsData(){
  var AWS_SECRET = '[AWS_SECRET]';
  var expiresDt = Math.floor(Date.now() / 1000) + (60 * 60 * 24);
  var path = '/MYFOLDER/ACTIVATION.csv'
  var stringToSign = 'GET\n\n\n' + expiresDt + '\n' + path;
  var hmac = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_1, AWS_SECRET,stringToSign, Utilities.Charset.UTF_8);
  var signature = encodeURIComponent(Utilities.base64Encode(hmac));
  var response = UrlFetchApp.fetch("http://s3-ap-southeast-2.amazonaws.com/MYFOLDER/ACTIVATION.csv?AWSAccessKeyId=[AWS_ACCESS_ID]&Expires="+expiresDt+"&Signature="+signature);
  var csvData = Utilities.parseCsv(response, ",");
  console.log(csvData);
  }

有人可以看看我哪里出问题了吗?我在这里关注文档:http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html

1 个答案:

答案 0 :(得分:0)

似乎我对hmac的签名不正确。

  var hmac = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_1, stringToSign, AWS_SECRET,Utilities.Charset.UTF_8);