gpt4 book ai didi

node.js - 无法使用 s3 getSignedUrl('getObject..) 下载图像并返回签名不匹配

转载 作者:搜寻专家 更新时间:2023-10-31 23:52:30 24 4
gpt4 key购买 nike

我对 AWS 比较陌生。我想要做的就是将图像从我的应用程序上传到 aws S3 并下载它以在应用程序的另一个页面中查看图像。上传成功,能够在S3中看到上传的图片。但无法下载它,因为它会抛出以下错误。

     FileTransferError {body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we
calculated does not match the signature you provided. Check your key and signing
method.</Message><AWSAccessKeyId>AKXXXXXXXXXXXXXXXXXX</AWSAccessKeyId>
<StringToSign>GET\n\n\n1469897687\n/huntuploads/uploads/%25222a85a6675d15eeeca5c8b
da6eed4c52e%2522</StringToSign>
<SignatureProvided>cUBhtiA5AGJbj8vl%2FX6xi%2B9BBRY%3D</SignatureProvided>
<StringToSignBytes>47 45 54 0a 0a 0a 31 34 36 39 38 39 37 36 38 37 0a 2f 68 75 6e
74 66 6f 6f 64 75 70 6c 6f 61 64 73 2f 75 70 6c 6f 61 64 73 2f 25 32 35 32 32 32
61 38 35 61 36 36 37 35 64 31 35 65 65 65 63 61 35 63 38 62 64 61 36 65 65 64 34
63 35 32 65 25 32 35 32 32</StringToSignBytes>
<RequestId>CCB513320456EB6B</RequestId>
<HostId>v4c7Ozf911tErWo5dCsL9RNLL78r3rUE6234Z801ZFXuELrji4juDehHmaxnK8t5qMBGcjz90a
I=</HostId></Error>";
code = 3;
"http_status" = 403;
source = "https://huntuploads.s3-us-west-
2.amazonaws.com/uploads/%25222a85a6675d15eeeca5c8bda6eed4c52e%2522?
AWSAccessKeyId=AKXXXXXXXXXXXXXXXXXX&Expires=1469897687&Signature=cUBhtiA5AGJbj8vl%
252FX6xi%252B9BBRY%253D";
target = "file:///var/mobile/Containers/Data/Application/1EB46B25-8BC5-
46C9-BE6B-BF1E120B7627/Documents/%222a85a6675d15eeeca5c8bda6eed4c52e%22";
}

下面是上传图片的代码,在服务器端运行良好

 var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: 'AXXXXXXXXX......', secretAccessKey: 'XXXXX....'});
AWS.config.update({region: 'us-west-2'});

var s3 = new AWS.S3( { params: {Bucket: 'huntfooduploads'}});

app.post('/FileUpload', function(req, res, next) {
var fileStream = fs.createReadStream(req.files.file.path);
var params = {
'Key': 'uploads/' + req.files.file.name,
'Body': fileStream,
'ContentEncoding': 'base64',
'Content-Type ': 'image/jpeg'
};

s3.upload(params, function(err, data) {
if (err) throw err;
console.log('after s3 upload====', err, data);
var imgFileInfo = req.files;
var imgUploadData = data;
....
}
}

以下代码用于使用 getSignedUrl 从服务器端为 s3 下载图像

var urlParams = {Bucket: 'huntuploads', Key:'uploads/'+rows[0].MyHunt_FileName};

// s3 getSigned Url
s3.getSignedUrl('getObject', urlParams, function(err, url) {
if (err) throw callback(err);
var fullUrl={awsUrl:url};
res.send(fullUrl);
})

一旦我将 fullUrl 发送回应用程序的客户端,我尝试使用以下内容

$cordovaFileTransfer.download(encodeURI(itemData[3].awsUrl), targetPath, options, trustHosts)
.then(function(result) {
console.log('Success! Download is successful');
$scope.imgURI = targetPath;
}, function(err) {
console.log('Error!!! Download is not successful');
// Error
}, function (progress) {
$timeout(function () {
$scope.downloadProgress = (progress.loaded / progress.total) * 100;
});
});

当我尝试下载图像时,从应用端收到上述消息。我尝试添加 nx-amz-server-side-encryption-customer-algorithm:AES256 作为 header 。我需要知道以下内容

  1. 这是将数据从服务器传递到客户端(app)的正确方法吗
  2. 如果 getSigned url 的 urlParams 正确。不确定我是否遗漏了什么或需要完成什么。

有人可以在正确的方向上给我建议吗。

最佳答案

好吧,我已经解决了自己的问题。但感谢迈克尔和其他人的帮助。解决方案是我在其中一个选项中添加了以下行

var options = {encodeURI:false};然后cordovaFileTransfer.download(encodeURI(itemData[3].awsUrl), targetPath, options, trustHosts){ ...}

解决了这个问题。看起来 encodeUI 默认设置为 true,这改变了签名。

关于node.js - 无法使用 s3 getSignedUrl('getObject..) 下载图像并返回签名不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com