使用Javascript AWS SDK生成预签名的S3 URL

时间:2017-06-13 21:42:22

标签: javascript amazon-web-services amazon-s3

我正在尝试允许从预签名网址访问资产,但我不确定如何执行此操作。从this page我可以看到我需要以下内容:

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>

我知道如何找到AWSAccessKeyIdYourSecretAccessKeyID,但如何使用HMAC-SHA1对其进行编码以形成最终签名?

1 个答案:

答案 0 :(得分:4)

我能够根据this Amazon S3 node example page生成一个预先签名的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
});