我正在尝试通过REST将Salesforce与AWS S3集成。
Attachment attach = [
select Body,
ContentType,
Name
from Attachment
limit 1
];
String attachmentBody = EncodingUtil.base64Encode(attach.Body);
String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
String key = 'xxxx';
String secret = 'xxxx';
String bucketname = 'kvntest';
String host = 's3-us-east-2.amazonaws.com';
String method = 'PUT';
String filename = attach.Id + '-' + attach.Name;
HttpRequest req = new HttpRequest();
req.setMethod(method);
req.setEndpoint('https://' + bucketname + '.' + host + '/' + bucketname + '/' + filename);
req.setHeader('Host', bucketname + '.' + host);
req.setHeader('Content-Length', String.valueOf(attachmentBody.length()));
req.setHeader('Content-Encoding', 'UTF-8');
req.setHeader('Content-type', attach.ContentType);
req.setHeader('Connection', 'keep-alive');
req.setHeader('Date', formattedDateString);
req.setHeader('ACL', 'public-read');
req.setBody(attachmentBody);
String stringToSign = 'PUT\n\n' +
attach.ContentType + '\n' +
formattedDateString + '\n' +
'/' + bucketname + '/' + bucketname + '/' + filename;
String encodedStringToSign = EncodingUtil.urlEncode(stringToSign, 'UTF-8');
Blob mac = Crypto.generateMac('HmacSHA256', blob.valueof(stringToSign),blob.valueof(secret));
String signed = EncodingUtil.base64Encode(mac);
String authHeader = 'AWS' + ' ' + key + ':' + signed;
req.setHeader('Authorization',authHeader);
String decoded = EncodingUtil.urlDecode(encodedStringToSign , 'UTF-8');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('*Resp:' + String.ValueOF(res.getBody()));
System.debug('RESPONSE STRING: ' + res.toString());
System.debug('RESPONSE STATUS: ' + res.getStatus());
System.debug('STATUS_CODE: ' + res.getStatusCode());
以上是我的匿名顶级代码。
出于某种原因,我继续得到这个错误响应,声明我需要使用SHA256,尽管我确实使用该算法进行加密。
<Message>The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.</Message><RequestId>C06368F096704377</RequestId><HostId>PloPZ5YnUD0vRyIg/uvcLRHFBpYoRBBVTCyEXIwK2wQvXUahnsPRnZxSqEyEARJeWVeVpS4fKuc=</HostId></Error>
有人可以给我一些关于我做错了什么的指示吗?
答案 0 :(得分:0)
us-east-2 only supports signature v4但似乎您正在使用签名v2。因此,您必须使用SigV4签署请求。可以找到一些示例here。希望这会有所帮助。