I want to use aws sdk (s3, connect) in apex that is why I asked. Is there anyway to implement it please share. I tried it in vf page, it works but for security I want to try it in apex. if there is no way to use sdk in apex, i have plan with rest api for aws service in apex. Is there is best way for REST API with s3 and connect please share here. Thanks.
You don't need SDK for this. You may push files from Salesforce to S3 using S3 API. Refer the URL for details: https://salesforcecodex.com/2020/01/uploading-files-to-s3-server-using-apex/
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
I am trying to access Amazon Connect api (GetDescribeUser) from Apex with REST API but I got following error. 403 Forbidden Unable to determine service/operation name to be authorized
I want to use aws sdk (s3, connect) in apex that is why I asked.
Is there anyway to implement it please share.
I tried it in vf page, it works but for security I want to try it in apex.
if there is no way to use sdk in apex, i have plan with rest api for aws service in apex.
Is there is best way for REST API with s3 and connect please share here. Thanks.
Regards,
LinThaw
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks, how about amazon connect? I want to try it look like s3 api.
https://www.any-api.com/amazonaws_com/connect/docs/_users_InstanceId_UserId_/DescribeUser
//To upload file to amazon S3
public static String uploadToAmazonS3 (String fileName, Blob attachmentBody, String fileContentType) {
String uploadFileName = fileName.replace(' ','_');
String formattedDateStr = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
String signed = FileUploadService.generateSignedPolicy(FileUploadService.AWS_BUCKET_NAME, formattedDateStr);
String uploadedFileURL ='FileUploadError';
String enCodedFileName = EncodingUtil.urlEncode(uploadFileName, 'UTF-8');
HttpRequest req = new HttpRequest();
req.setMethod('PUT');
req.setHeader('Host', FileUploadService.AWS_HOST);
String finalURL = 'https://' + FileUploadService.AWS_HOST + '/' + enCodedFileName;
req.setEndpoint(finalURL);
req.setHeader('Content-Length','50' );//String.valueOf(attachmentBody.toString().length())
req.setHeader('Content-Type', fileContentType);
req.setHeader('Connection', 'keep-alive');
req.setHeader('date', formattedDateStr);
req.setHeader('x-amz-acl', 'public-read');
//get signature string
String stringToSign = 'PUT\n\n'+
fileContentType + '\n' +
formattedDateStr+'\nx-amz-acl:public-read\n/'+
FileUploadService.AWS_BUCKET_NAME+'/'+
enCodedFileName;
string sig;
Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(stringToSign),blob.valueof(FileUploadService.AWS_SECRET_KEY));
sig = EncodingUtil.base64Encode(mac);
String authHeader = 'AWS' + ' ' + FileUploadService.AWS_ACCESS_KEY + ':' + sig;
req.setHeader('Authorization',authHeader);
req.setBodyAsBlob(attachmentBody);
Http http = new Http();
HTTPResponse res = new HTTPResponse();
try {
//Execute web service call
res = http.send(req);
if(res.getStatusCode()==200){
uploadedFileURL = finalURL;
} else{
SystemLogHelper.insertLog('S3FileUpload',false,'', 'Error occured - ' + res.getStatus(), '',null,true);
}
return uploadedFileURL;
} catch(System.CalloutException e) {
system.debug('AWS Service Callout Exception: ' + e.getMessage());
uploadedFileURL = 'FileUploadError';
SystemLogHelper.insertLog('S3FileUpload',false,'', 'Error occured - ' + e.getMessage(), '',null,true);
return uploadedFileURL;
}
}
403 Forbidden Unable to determine service/operation name to be authorized
Anyone help will be appreciated.
Regards,
LinThaw