Pre Requisites:
Bucket name: testbucket1
Region: us-east-2
Access key ID: XXXXXXXXXXXXXXXXXX
Secret access key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
NOTE
1. Aws s3 storage server, storing the data only in the file format (Ex: Upload-Test-3.xml).
2. PUT is used to create the record or update records in s3 bucket
3. Host header is followed by bucket, region, and base URL (Ex:s3.us-east-2.amazonaws.com).
1. Insert the Data into Amazon s3 bucket through XML Payload
public class AWSS3 {
public void uploadToAmazonS3() {
String xmlStringxmlRes= '<?xml version="1.0" encoding="UTF-8">'+
'<my:myField>'+
'<my:LANID>12345678910</my:LANID>'+
'</my:myField>';
Blob xmlContentBlob = Blob.valueOf(xmlStringxmlRes);
String attachmentBody = EncodingUtil.base64Encode(xmlContentBlob);
String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
String key = 'XXXXXXXXXXXXXXXXX'; //Access key ID
String secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; //Secret access key
//https://testmay21.s3.us-east-2.amazonaws.com/
String bucketname = 'testmay21'; //Bucket Name
String host = 's3.us-east-2.amazonaws.com';
String method = 'PUT'; //Create a file in Amazon s3 bucket
String filename = 'Upload-Test-3.xml'; //Give any Name
String fileType = 'text/xml'; //text/xml
HttpRequest req = new HttpRequest();
req.setMethod(method);
req.setEndpoint('callout:AWS_S3' + '/' + filename); //Named credentials (Refer below 'Screenshot')
req.setHeader('Host', bucketname + '.' + host);
req.setHeader('Content-Length', String.valueOf(attachmentBody.length()));
req.setHeader('Content-Encoding', 'UTF-8');
req.setHeader('Content-type', fileType);
req.setHeader('Connection', 'keep-alive');
req.setHeader('Date', formattedDateString);
req.setHeader('ACL', 'public-read-write');
req.setBodyAsBlob(EncodingUtil.base64Decode(attachmentBody));
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('>>>> The response');
System.debug(res);
System.debug('>>>> The body');
System.debug(res.getBody());
System.debug('>>>> Status code');
System.debug(res.getStatusCode());
System.debug('>>>> Finished method');
}
}
Screenshot
2. Get data from the amazon s3 bucket.
Endpoint Url : https://s3.us-east-2.amazonaws.com/buckename/Upload-Test-3.xml
Authorization :
Reference links: