最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AuthorizationHeaderMalformed when trying to upload object to S3 via JS SDK - Stack Overflow

programmeradmin1浏览0评论

I’m trying to upload an object to Amazon S3 via their JavaScript SDK, but getting the following error during the upload:

<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 /?max-keys=0 is made.

I’ve tried setting the region everywhere, but still getting the error.

Here’s what my code instantiating the S3 client looks like:

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?

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);

I’m trying to upload an object to Amazon S3 via their JavaScript SDK, but getting the following error during the upload:

<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./?max-keys=0 is made.

I’ve tried setting the region everywhere, but still getting the error.

Here’s what my code instantiating the S3 client looks like:

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?

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);
Share Improve this question edited Jan 15, 2018 at 22:42 Martin Bean asked Jan 15, 2018 at 19:44 Martin BeanMartin Bean 39.5k28 gold badges132 silver badges206 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

The issue is caused by your call to CognitoIdentityCredentials The variable AWS is not setup yet.

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.

Since S3 uses a single namespace all bucket names have to be unique. 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.

发布评论

评论列表(0)

  1. 暂无评论