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.setMethod('PUT');
awsReq.setEndpoint('callout:aws_s3_ap');
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');
awsReq.setBodyAsBlob(EncodingUtil.base64Decode(attachmentBody));
System.debug('******************* Start sending web request to S3 *******************');
Http awsHttp = new Http();
HttpResponse awsResponseMessage = awsHttp.send(awsReq);
}
}
I set the named credentials as follow;
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?