Creating a signed S3 URL with Javascript

前端 未结 3 957
情书的邮戳
情书的邮戳 2021-02-04 18:54

I am attempting to create a signed S3 URL using Javascript & NodeJS. I have used this specification.

var crypto     = require(\'crypto\'),
    date       =          


        
3条回答
  •  隐瞒了意图╮
    2021-02-04 19:29

    I would try using Knox along with Node.Js . Its known to be a great combination and also itself utilizes the Node.JS Crypto library which is kind of what you're trying to do - saving you time:)

    More info here : https://github.com/LearnBoost/knox

    Than, you could just do something like:

    var knox = require('knox');
    var s3Client = knox.createClient({
        key: 'XXX',
        secret: 'XXX',
        bucket: 'XXX'
    });
    
    var expires = new Date();
    expires.setMinutes(expires.getMinutes() + 30);
    var url =  s3Client.signedUrl(filename, expires);
    

    Edit: You could also look into Knox and just check what the signedUrl function does and implement that yourself.Than you could add to the auth.signQuery call an extra option called amazonHeaders:

    Client.prototype.signedUrl = function(filename, expiration){
      var epoch = Math.floor(expiration.getTime()/1000);
      var signature = auth.signQuery({
        amazonHeaders: 'response-content-disposition:attachment',
        secret: this.secret,
        date: epoch,
        resource: '/' + this.bucket + url.parse(filename).pathname
      });
    
      return this.url(filename) +
        '?Expires=' + epoch +
        '&AWSAccessKeyId=' + this.key +
        '&Signature=' + encodeURIComponent(signature);
    };
    

    Shai.

提交回复
热议问题