'The specified method is not allowed against this resource in Amazon aws

Guys I may or may not be wrong, But seriously, I am struggling with file uploading problem in Amazon S3 bucket. When I am trying to hit on the request then I am getting the following error.

MethodNotAllowed and The specified method is not allowed against this resource

The above message is the sort of the below response.

<?xml version="1.0" encoding="UTF-8"?><Error><Code>MethodNotAllowed</Code
<Message>Thespecified method is not allowed against this resource.</Message>
<Method>POST</Method><ResourceType>OBJECT</ResourceType>
<RequestId>xxx</RequestId><HostId>xxxx</HostId></Error>

The above message is the complete message and below is the code whatever I have written for uploading files to amazon s3 server.

public synchronized boolean uploadFile(String url, String name, File file) {
    HttpEntity entity = MultipartEntityBuilder.create()
            .addPart("file", new FileBody(file)).build();
    HttpPost request = new HttpPost(url);
    request.setEntity(entity);
    HttpClient client = HttpClientBuilder.create().build();
    try {
        HttpResponse response = client.execute(request);
        entity = response.getEntity();
        if (entity != null) {
            try (InputStream in_stream = entity.getContent()) {
                BufferedReader in = new BufferedReader(new InputStreamReader(in_stream));
                String inputLine;
                StringBuilder responseBuffer = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    responseBuffer.append(inputLine);
                }
                in.close();
                String a = responseBuffer.toString();
                Utils.print("\n\n" + a + "\n\n");
            }
        }
        return true;
    } catch (Exception e) {
        Utils.print(e);
    }
    return false;
}

Please suggest me what to do for this? I will be very thankful for your expected answer.



Solution 1:[1]

You could be getting MethodNotAllowed, which recommends using an identity that belongs to the bucket owner's account.

If you have the correct permissions, but you're not using an identity that belongs to the bucket owner's account, Amazon S3 returns a 405 Method Not Allowed error. https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketPolicy.html

from a list of S3 API error responses

The specified method is not allowed against this resource.
https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nicole Caldwell