S3 POST request to S3 with response 405
Asked Answered
L

2

7

I have the following CORS configuration for S3 in order to use one of my buckets as a static website hosting:

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

Then I have the following Edit Redirection Rules:

<RoutingRules>
    <RoutingRule>
    <Condition>
        <KeyPrefixEquals>abc</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <HostName>myec2instance.com</HostName>
    </Redirect>
</RoutingRule>

What I want to do is when S3 receives a POST to /abc redirects the request and request body to my ec2 instance. The redirection rule is working properly (I was able to test this by switching POST to a GET request) but for any reason S3 is returning HTTPResponse 405 when the request is a POST. Any ideas why?

Ladin answered 18/2, 2015 at 20:43 Comment(2)
The references I can find to this error following a redirect seem to indicate that it happens if you redirect either to a folder instead of a file, or a folder that doesn't have a default document set. Is it either of those?Wanigan
Hi @monkeymatrix thanks for your answer! My problem is not the redirect operation (at least I think so). The problem for me is that S3 as a static website host does not allow me to send a HTTP POST from the javascript that I have inside the webpage. What I'm trying to accomplish is to create a landing page where users can leave their contact then the webpage send their contact to S3 by POST and finally S3 redirects to an ec2 instance that saves the json data sent.Ladin
R
14

This isn't CORS related -- it's S3 itself. The S3 website endpoints are only equipped for GET and HEAD. Anything else should be denied before the redirection rules are checked.

Website Endpoint

Supports only GET and HEAD requests on objects.

http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteEndpoints.html

Rhoea answered 19/2, 2015 at 12:31 Comment(4)
So does this mean you can't use S3 website hosting to forward POST requests?Vershen
@Vershen - correct... although I'm not sure why you'd want to "forward" (redirect?) a POST because that would mean re-sending the request body to the new location, costing time and bandwidth at the browser side. You may want to create a new question, here, describing your use case and what problem you're trying to solve. POST requests to the S3 web site endpoints, even in the presence of a matching redirect rule, is met with 405 Method Not Allowed.Rhoea
Thanks Michael. I'm hitting a backend REST API from a frontend app hosted on S3. I realized the right way to do it is hitting the backend directly with CORS, without needing a redirect.Vershen
U can use API gateway, Lambda.Agram
P
1

If you want to use POST request to S3, you need to use root object in your browser by creating POSTPolicy to get S3 authentication

you can refer Browser-Based Upload using HTTP POST

Pungy answered 5/12, 2019 at 5:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.