I have a class that should invoke a web request to S3 to upload a JSON object, but I received the following error

Response from s3 System.HttpResponse[Status=Method Not Allowed, StatusCode=405]
20:57:04:000 USER_DEBUG <Error><Code>MethodNotAllowed</Code><Message>The specified method is not allowed against this resource.</Message><Method>PUT</Method></Error>

I checked these articles 1 & 2 but still couldn't resolve my problem.

Below is the code;

public class awsBucketManager{

    @future(callout = true)
    public static void requestAPItoS3(String body, String country){

        Blob JSONBodyBlob = Blob.valueOf(body);
        String attachmentBody = EncodingUtil.base64Encode(JSONBodyBlob);
        String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
        String key = 'xxxxx'; //key
        String secret = 'xxxxxx'; //secret
        String bucketname = 'bucket-name-use1';
        String host = 's3.us-east-1.amazonaws.com';
        String method = 'PUT';
        String fileType = 'text/json';

        HttpRequest awsReq = new HttpRequest();
        awsReq.setHeader('Host', bucketname + '.' + host);
        awsReq.setHeader('Content-Length', String.valueOf(attachmentBody.length()));
        awsReq.setHeader('Content-Encoding', 'UTF-8');
        awsReq.setHeader('Content-type', fileType);
        awsReq.setHeader('Connection', 'keep-alive');
        awsReq.setHeader('Date', formattedDateString);
        awsReq.setHeader('ACL', 'public-read-write');

        System.debug('******************* Start sending web request to S3 *******************'); 
        Http awsHttp = new Http();
        HttpResponse awsResponseMessage = awsHttp.send(awsReq);

I set the named credentials as follow; enter image description here

An overview of my S3 bucket config Block public access (bucket settings) -> it's all off Bucket policy -> it's empty ACL -> List Write & Read Write only for the bucket owner

What could be the solution to this problem?


Most likely, your endpoint is wrong. You need to specify an action that you're doing, such as:


Where awsAction might be something like:

 String awsAction = 'myFile';

This would be an action for PutObject, for example.

You need to consult the documentation to determine the correct endpoint to use.

Not the answer you're looking for? Browse other questions tagged or ask your own question.