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

javascript - 400 Bad Request from PUT to S3 using pre-signed URL - Stack Overflow

programmeradmin1浏览0评论

I am trying to upload a video file from JS to an S3 bucket but am getting a 400 bad request right now. I am sure I set up everything properly but could of done something.

I generate a URL on a server like this:

const client = new S3({
    accessKeyId: 'id-ommited',
    secretAccessKey: 'key-ommited',
    region: 'eu-west-2',
    useAccelerateEndpoint: true
});
const url = client.getSignedUrl('putObject', {
    Bucket:  'screen-capture-uploads',
    Key:     'video.webm',
    Expires: 60 * 60,
    ContentType: 'application/octet-stream'
});

This URL appears to be fine on my frontend which I put through to an XMLHttpRequest like so

const xhr = new XMLHttpRequest();

xhr.open('PUT', body.url); // body.url being the presigned url
xhr.setRequestHeader('x-amz-acl', 'public-read');
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.send(blob); // blob being the webm binary

I am using a CORS setup that just allows PUT from anywhere

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

I checked that and it works fine. Also the IAM user has permission to put to the bucket

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::screen-capture-uploads/*"
            ]
        }
    ]
}

I managed to solve my issues up to this point. I've looked at some other answers but didn't find anything useful for me. Anybody got any ideas for me?

I am trying to upload a video file from JS to an S3 bucket but am getting a 400 bad request right now. I am sure I set up everything properly but could of done something.

I generate a URL on a server like this:

const client = new S3({
    accessKeyId: 'id-ommited',
    secretAccessKey: 'key-ommited',
    region: 'eu-west-2',
    useAccelerateEndpoint: true
});
const url = client.getSignedUrl('putObject', {
    Bucket:  'screen-capture-uploads',
    Key:     'video.webm',
    Expires: 60 * 60,
    ContentType: 'application/octet-stream'
});

This URL appears to be fine on my frontend which I put through to an XMLHttpRequest like so

const xhr = new XMLHttpRequest();

xhr.open('PUT', body.url); // body.url being the presigned url
xhr.setRequestHeader('x-amz-acl', 'public-read');
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.send(blob); // blob being the webm binary

I am using a CORS setup that just allows PUT from anywhere

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws./doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

I checked that and it works fine. Also the IAM user has permission to put to the bucket

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::screen-capture-uploads/*"
            ]
        }
    ]
}

I managed to solve my issues up to this point. I've looked at some other answers but didn't find anything useful for me. Anybody got any ideas for me?

Share Improve this question asked Jul 16, 2019 at 13:34 Reece WardReece Ward 2231 gold badge2 silver badges9 bronze badges 1
  • Man, you are god! I just changed the content-type and it worked like a charm – Ganesh Chowdhary Sadanala Commented May 12, 2024 at 2:34
Add a ment  | 

1 Answer 1

Reset to default 4

I went and got the endpoint it produced and just put it straight in the browser. Turns out I hadn't enabled transfer acceleration on the bucket I was using!

This just turned into a bad request without any extra information

发布评论

评论列表(0)

  1. 暂无评论