问题描述
标题说明了一切.这是我的代码;
我正在使用节点可格式化文件.
form.on("end",function(field, file){
params.Body = fs.createReadStream(params.filePath)
delete params.filePath;
S3.getSignedUrl('putObject',params, function(err, url) {
if(err){console.log(err);}
console.log(url);
});
})
上传成功后,url变量返回s3 url,类似这样;
但仍然出现SignatureDoesNotMatch错误.在说明中说
这是我的参数
params = {
Bucket:"bucketname",
Key: file.name,
ACL: 'public-read'
}
我想念什么?
在服务器端使用S3.getSignedUrl('putObject'
,然后尝试在客户端使用该URL时遇到了相同的问题.
在我的案例中,我注意到的可能与您的情况有关的是,使用所有S3.getSignedUrl
创建的签名都考虑了请求标头.因此,如果您生成的是URL,除非使用相同的标题发送,否则它将失败,并显示与您收到的相同的错误消息.
一个失败的例子:像这样生成.
var params = { Bucket: 'YourBucket', Key: 'uniqueFileKey', Expires: 10000 };
s3.getSignedUrl('putObject', params, function (err, url) {
if(err){
return cb(err);
}
return cb(null, url)
});
当使用相同的URL生成以下请求失败.该请求是从浏览器发出的.
RequestMethod: Put
Headers: {
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.9
Connection:keep-alive
Content-Length:11768
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
}
区别在于,上面创建的签名不包含content-type,其中请求确实指定了content-type.参数需要匹配标题,否则抛出的错误将是签名不匹配.
下面的成功示例:
var params = { Bucket: 'YourBucket', Key: 'uniqueFileKey', Expires: 10000, Content-Type: 'application/x-www-form-urlencoded; charset=UTF-8' };
s3.getSignedUrl('putObject', params, function (err, url) {
if(err){
return cb(err);
}
return cb(null, url)
});
Title says everything. Here is my code;
I'm using node-formidable for files.
form.on("end",function(field, file){
params.Body = fs.createReadStream(params.filePath)
delete params.filePath;
S3.getSignedUrl('putObject',params, function(err, url) {
if(err){console.log(err);}
console.log(url);
});
})
After successful upload, url variable returns s3 url, something like this;
But still getting SignatureDoesNotMatch error. In description says
Here is my parameters
params = {
Bucket:"bucketname",
Key: file.name,
ACL: 'public-read'
}
What am i missing?
I ran into the same problem while using S3.getSignedUrl('putObject'
, serverside, and then trying to use that url clientside.
What I noticed in my case, which might be relevant to yours, is that signatures created with all the S3.getSignedUrl
take into account request headers. So if you are generating a URL, it will fail with the same error message you received unless sent with the same headers.
One example of a failure: Generated like this..
var params = { Bucket: 'YourBucket', Key: 'uniqueFileKey', Expires: 10000 };
s3.getSignedUrl('putObject', params, function (err, url) {
if(err){
return cb(err);
}
return cb(null, url)
});
The following request fails when a using that same url generated. This request was made from a browser.
RequestMethod: Put
Headers: {
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.9
Connection:keep-alive
Content-Length:11768
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
}
And the difference is that the signature above created doesn't include content-type, where the request does specify a content-type. Params need to match headers, or the error thrown will be signature doesn't match.
Successful example below:
var params = { Bucket: 'YourBucket', Key: 'uniqueFileKey', Expires: 10000, Content-Type: 'application/x-www-form-urlencoded; charset=UTF-8' };
s3.getSignedUrl('putObject', params, function (err, url) {
if(err){
return cb(err);
}
return cb(null, url)
});
这篇关于预签名的S3 URL签名不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!