使用 Javascript AWS 开发工具包生成预签名的 S3 URL

Jer*_*mas 2 javascript amazon-s3 amazon-web-services

我试图允许从预先签名的 URL 访问资产,但我不确定如何执行此操作。从这个页面我可以看到我需要以下内容:

Authorization = "AWS" + " " + AWSAccessKeyId + ":" + Signature;

Signature = Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of( StringToSign ) ) );

StringToSign = HTTP-Verb + "\n" +
  Content-MD5 + "\n" +
  Content-Type + "\n" +
  Date + "\n" +
  CanonicalizedAmzHeaders +
  CanonicalizedResource;

CanonicalizedResource = [ "/" + Bucket ] +
  <HTTP-Request-URI, from the protocol name up to the query string> +
  [ subresource, if present. For example "?acl", "?location", "?logging", or "?torrent"];

CanonicalizedAmzHeaders = <described below>
Run Code Online (Sandbox Code Playgroud)

我知道如何找到 theAWSAccessKeyId和 theYourSecretAccessKeyID但我如何使用它进行编码HMAC-SHA1以形成最终签名?

Jer*_*mas 5

我能够根据这个 Amazon S3 节点示例页面生成一个预先签名的 URL 来获取文件:

declare var AWS:any;

AWS.config.accessKeyId = "key_goes_here";
AWS.config.secretAccessKey = "secret_key_goes_here";
AWS.config.region = "region_goes_here";

params = {
    Bucket: 'bucket_name_goes_here',
    Key:  'path_to_file_goes_here'
}

s3.getSignedUrl('getObject', params, function (err, url) { 
    // Do some sort of processing with url
});
Run Code Online (Sandbox Code Playgroud)