我试图在ColdFusion中使用s3的put对象复制功能(s3替换文件)和用户定义的元数据。我正在使用Joe Danziger的函数(https://gist.github.com/CFJSGeek/3f6f14ba86049af75361)来创建签名。 但是我一直收到我的签名不匹配的错误。有任何想法吗? 这是我的代码:
private binary function HMAC_SHA1(required string signKey, required string signMessage){
var jMsg = JavaCast("string",arguments.signMessage).getBytes("iso-8859-1");
var jKey = JavaCast("string",arguments.signKey).getBytes("iso-8859-1");
var key = createObject("java","javax.crypto.spec.SecretKeySpec");
var mac = createObject("java","javax.crypto.Mac");
key = key.init(jKey,"HmacSHA1");
mac = mac.getInstance(key.getAlgorithm());
mac.init(key);
mac.update(jMsg);
return mac.doFinal();
}
private string function createSignature(required string stringIn){
var fixedData = replace(arguments.stringIn,"\n","#chr(10)#","all");
var digest = HMAC_SHA1(variables.awsSecretKey, fixedData);
var signature = ToBase64("#digest#");
return signature;
}
Joe Danziger的代码(我在脚本中重写了):
{{1}}
答案 0 :(得分:2)
我解决了这个问题: 标题需要按字母顺序排序! (当传入createSignature函数时......它似乎没有给出实际的http标题的问题) 这是我的更新代码:
public function setMetaDataForObject(required string bucket, required string objectKey, struct metadata, string contentType){
if(!isdefined("argumetns.contentType")){
var meta = getMetaDataForObject(arguments.bucket, arguments.objectKey);
arguments.contentType = meta['Content-type'];
}
var dateTimeString = GetHTTPTimeString(now());
var cs = "PUT\n\n#arguments.contentType#\n#dateTimeString#\nx-amz-copy-source:/#arguments.bucket#/#arguments.objectKey#\n";
if(isdefined("arguments.metadata")){
for(key in structSort(arguments.metadata)){
cs &= "#key#:#metadata[key]#\n";
}
}
cs &= "x-amz-metadata-directive:REPLACE\n/#arguments.bucket#/#arguments.objectKey#";
var signature = createSignature(cs);
var httpReq = new http();
httpReq.setMethod("PUT");
httpReq.setUrl("http://#arguments.bucket#.s3.amazonaws.com/#arguments.objectKey#" );
httpReq.addParam(type="header", name="Date", value="#dateTimeString#");
httpReq.addParam(type="header", name="Content-Type", value="img/png");
httpReq.addParam(type="header", name="x-amz-copy-source", value="/#arguments.bucket#/#arguments.objectKey#");
httpReq.addParam(type="header", name="x-amz-metadata-directive", value="REPLACE");
httpReq.addParam(type="header", name="Authorization", value="AWS #variables.accessKeyId#:#signature#");
if(isdefined("arguments.metadata")){
for(var i in arguments.metadata){
httpReq.addParam(type="header", name="#i#", value="#arguments.metadata[i]#");
}
}
var result = httpReq.send().getPrefix(); writedump(result);
}