一直在try 使用预先签名的url将图像从React Native上传到S3,但没有成功.这是我的代码:
在 node 中生成预签名的url:
const s3 = new aws.S3();
const s3Params = {
Bucket: bucket,
Key: fileName,
Expires: 60,
ContentType: 'image/jpeg',
ACL: 'public-read'
};
return s3.getSignedUrl('putObject', s3Params);
以下是RN对S3的请求:
var file = {
uri: game.pictureToSubmitUri,
type: 'image/jpeg',
name: 'image.jpg',
};
const xhr = new XMLHttpRequest();
var body = new FormData();
body.append('file', file);
xhr.open('PUT', signedRequest);
xhr.onreadystatechange = () => {
if(xhr.readyState === 4){
if(xhr.status === 200){
alert('Posted!');
}
else{
alert('Could not upload file.');
}
}
};
xhr.send(body);
游戏pictureToSubmitUri=assets-library://asset/asset.JPG?id=A282A2C5-31C8-489F-9652-7D3BD5A1FAA4&ext=JPG
signedRequest=https://my-bucket.s3-us-west-1.amazonaws.com/8bd2d4b9-3206-4bff-944d-e06f872d8be3?AWSAccessKeyId=AKIAIOLHQY4GAXN26FOQ&Content-Type=image%2Fjpeg&Expires=1465671117&Signature=bkQIp5lgzuYrt2vyl7rqpCXPcps%3D&x-amz-acl=public-read
错误消息:
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your key and signing method.
</Message>
我可以使用生成的url成功地将其curl 并镜像到S3,并且似乎能够成功地发布到requestb.in from RN(但是我只能在requestb.in上看到原始数据,所以不能100%确定图像是否正确).
基于所有这些,我把问题缩小到1)我的图像上传周期不正确,或者2)S3希望我的请求的方式与它的输入方式不同.
任何帮助都将不胜感激!
使现代化
如果正文只是文本({'data':'foo'}),则可以成功地从RN发布到S3.也许AWS不喜欢多格式数据?我怎样才能在RN中以文件的形式发送???