简体   繁体   English

尝试通过 JS SDK 将对象上传到 S3 时 AuthorizationHeaderMalformed

[英]AuthorizationHeaderMalformed when trying to upload object to S3 via JS SDK

I'm trying to upload an object to Amazon S3 via their JavaScript SDK, but getting the following error during the upload:我正在尝试通过他们的 JavaScript SDK 将对象上传到 Amazon S3,但在上传过程中出现以下错误:

<Error>
    <Code>AuthorizationHeaderMalformed</Code>
    <Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'eu-west-1'</Message>
    <Region>eu-west-1</Region>
    <RequestId>62D2D18E5093BF1D</RequestId>
    <HostId>87ixJCkZyInIVI9BH4zdxtNzFuydwByK6ibvXOICXoE6ZQMp+lWf9RxetaL9c5qFEZEWW/RYdFQ=</HostId>
</Error>

I get this error response when a HTTP request to https://my-bucket-name.s3.amazonaws.com/?max-keys=0 is made.当向https://my-bucket-name.s3.amazonaws.com/?max-keys=0发出 HTTP 请求时,我收到此错误响应。

I've tried setting the region everywhere, but still getting the error.我试过在任何地方设置区域,但仍然出现错误。

Here's what my code instantiating the S3 client looks like:这是我实例化 S3 客户端的代码的样子:

import AWS from 'aws-sdk';

AWS.config.update({
    region: 'eu-west-1',
    credentials: new AWS.CognitoIdentityCredentials({
        IdentityPoolId: this.identityPoolId
    }, {
        region: 'eu-west-1'
    })
});

this.s3 = new AWS.S3({
    apiVersion: '2006-03-01',
    params: {
        Bucket: this.bucket
    },
    region: 'eu-west-1'
});

Where am I going wrong?我哪里错了? Why is there still a reference to us-east-1 in the above error response when generating the signature?为什么在生成签名时上面的错误响应中还有对us-east-1的引用?

EDIT: I've written and re-written the set-up code a few times over now.编辑:我已经编写并重新编写了几次设置代码。 This is what I currently have:这是我目前拥有的:

const AWS = require('aws-sdk/global');
const S3 = require('aws-sdk/clients/s3');

this.s3 = new S3({
    apiVersion: 'latest',
    credentials: new AWS.CognitoIdentityCredentials({
        IdentityPoolId: this.identityPoolId
    }, {
        region: 'eu-west-1'
    }),
    params: {
        Bucket: this.bucket
    },
    region: 'eu-west-1'
});

And then the code that does the actual file upload:然后是执行实际文件上传的代码:

const params = {
    ACL: 'private',
    Body: this.file,
    ContentType: this.file.type,
    Key: `videos/input/${this.filename}`
};

this.s3
    .putObject(params)
    .on('httpUploadProgress', this.onUploadProgress)
    .send(this.onSend);

The issue is caused by your call to CognitoIdentityCredentials The variable AWS is not setup yet.该问题是由您调用CognitoIdentityCredentials引起的。变量 AWS 尚未设置。

Change your code to look like this:将您的代码更改为如下所示:

AWS.config.region = 'eu-west-1';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: this.identityPoolId
});

When you get this error it means that the bucket name you have chosen exists somewhere else on Amazon S3, this could be in your account or in another account.当您收到此错误时,表示您选择的存储桶名称存在于 Amazon S3 上的其他位置,这可能在您的账户或另一个账户中。

Since S3 uses a single namespace all bucket names have to be unique.由于 S3 使用单个命名空间,因此所有存储桶名称都必须是唯一的。 If this bucket exists inside your account update your script to point to the actual region where the bucket exists.如果此存储桶存在于您的账户中,请更新您的脚本以指向存储桶所在的实际区域。 If it's not in your account then change the bucket name and give it a go.如果它不在您的帐户中,请更改存储桶名称并试一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM