亚马逊s3授权标题无效

时间:2015-01-23 09:34:45

标签: node.js amazon-web-services amazon-s3

我遇到亚马逊s3身份验证问题。

错误指向亚马逊授权。 它说。

AWS授权标头无效。预计AwsAccessKeyId:签名

这里是我的代码:

   // set default values for optional params first
    bucket = bucket || null;
    path = path || '/';
    query = query || [];
    data = data || null;
    headers = headers || [];
    amazon = amazon || null;

    //get host - bucket1.s3.amazonaws.com
    var host    = this._getHost(bucket);
    //get url - http://bucket1.s3.amazonaws.com/some/path
    var url     = this._getUrl(host, path, query);
    //get path - /bucket1/some/path
    var path    = this._getPath(bucket, path);

    hashed().ksort(amazon);

    // initiatialize variables needed
    var restHeaders = amazonHeaders = [];

    headers.Host = host;
    headers.Date = time().toDate(Date.now(), 'D, d M Y H:i:s T', true);

    for (var header in amazon) {
        restHeaders.push(header+': '+amazon[header]);
        amazonHeaders.push(header.toLowerCase()+':'+amazon[header]);
    }

    for (var header in headers) {
        restHeaders.push(header+': '+headers[header]);
    }

    amazonHeaders = '\n'+amazonHeaders.join('\n');

    if(!amazonHeaders.trim()) {
        amazonHeaders = null;
    }

    if(!(typeof headers['Content-MD5'] !== 'undefined')) {
        headers['Content-MD5'] = null;
    }

    if(!(typeof headers['Content-type'] !== 'undefined')) {
        headers['Content-Type'] = null;
    }

    //get signature
    var signature = [
        action,
        headers['Content-MD5'], 
        headers['Content-Type'],
        headers['Date']+amazonHeaders,
        path];

    signature = signature.join('\n');
    if(headers.Host == 'cloudfront.amazonaws.com') {
        signature = headers.Date;
    }

    restHeaders.push('Host: '+host);
    restHeaders.push('Date: '+headers.Date);
    restHeaders.push('Authorization: '+this._getSignature(signature));

请帮助我,我永远坚持这一点。

1 个答案:

答案 0 :(得分:0)

对于大多数传统用例和应用程序,您不应自己编写代码来计算签名。您应该使用AWS提供的SDK作为编程语言

请参阅http://aws.amazon.com/tools

这些SDK提供更高级别的功能,更易于使用。他们管理许多低级细节,例如连接管理,错误处理和......签名!