Skip to main content
 首页 » 编程设计

python之当 python 中的相同请求正在工作时,无法使用 aws4 在 native 中上传图片

2024年12月31日14qlqwjy

我的任务是通过 React Native 将图片上传到 API。身份验证由 AWS 完成,但没有 cognito。所以我正在通过 aws.sts.requestRoleWithSAML 请求一个角色,女巫目前正在工作。现在,我通过使用模块“aws4-react-native”使用 AWS 签名版本 4 key 签署我的请求,然后向 API 发送一个 fetch-post 请求。 但我就是无法让它工作。我收到的错误信息是:

“我们计算出的请求签名与您提供的签名不匹配。请检查您的 AWS secret 访问 key 和签名方法。有关详细信息,请参阅服务文档。”

所以也许我在这里做错了什么,但我真正不明白的是我得到了一个完全可用的 python 示例。

也许有人可以帮我解决这个问题,或者可以简单地向我提供有关如何将 python 部分从 RN 转换为 javascript 的信息。

python 代码:

client = boto3.client('sts') 
temp_credentials = client.assume_role_with_saml( 
            RoleArn='xxxxxx', 
            PrincipalArn=''xxxxx', 
            SAMLAssertion=saml_response) 
 
auth = AWSRequestsAuth( 
   aws_access_key=temp_credentials['Credentials']['AccessKeyId'], 
   aws_secret_access_key=temp_credentials['Credentials'] 
                                      ['SecretAccessKey'], 
   aws_host='xxxxx', 
   aws_region='us-east-1', 
   aws_service='execute-api', 
   aws_token=temp_credentials['Credentials']['SessionToken']) 
 
   URL = 'xxxxx' 
   files = {'picture': open('IMG_20180326_193921.jpg', 'rb')} 
header = { 
   "version_code": "1", 
   "crop": "MAIZE", 
   "device_locale": "US", 
   "date": "20180427_173034", 
   "peat_id": "200001", 
   "user_id": "fakeid25-e6a2-4e0c-9514-ccf429973af5", 
   "pla_id": "fakeid05-19ab-4896-b748-e1c1ab99b1a9", 
   "pic_id": "fakeidee-752f-42f0-a4e7-3e3e6fed9d76", 
   "images_captured": "1", 
   "images_uploaded": "0", 
   "latitude": "10.15", 
   "longitude": "10.15", 
   "accuracy": "364", 
   "file_name": "my-photo.tif", 
   "app_locale": "US", 
   "email": "some.person@pioneer.com", 
   "user_name": "username", 
   "confidence": "HIGH", 
   "severity": "HIGH", 
   "identifier": "Fake Image", 
   "notes": "Blah blah blah" 
} 
response = requests.post(URL, files=files, headers=header, auth=auth) 

react native 代码:

const data = new FomrData(); 
data.append('picture', { uri: 'path/to/test.jpg', name: 'test.jpg',  
                        type: 'image/jpg' }); 
let temp = data.Credentials 
let keyID = temp.AccessKeyId 
let secretKey = temp.SecretAccessKey 
let st = temp.SessionToken 
 
var opts = { 
    host: 'xxxxx', 
    path: '/image_analysis', 
    region: 'us-east-1', 
    service: 'execute-api', 
    headers: { 
       'Host': 'xxxxx', 
       'version_code': '1', 
       'crop': 'MAIZE', 
       'device_locale': 'US', 
       'date': '20180427_173034', 
       'peat_id': '200001', 
       'user_id': 'fakeid25-e6a2-4e0c-9514-ccf429973af5', 
       'pla_id': 'fakeid05-19ab-4896-b748-e1c1ab99b1a9', 
       'pic_id': 'fakeidee-752f-42f0-a4e7-3e3e6fed9d76', 
       'images_captured': '1', 
       'images_uploaded': '0', 
       'latitude': '10.15', 
       'longitude': '10.15', 
       'accuracy': '364', 
       'file_name': imgname, 
       'app_locale': 'US', 
       'email': 'some.person@pioneer.com', 
       'user_name': 'username', 
       'confidence': 'HIGH', 
       'severity': 'HIGH', 
       'identifier': 'Fake Image', 
       'notes': 'Blah blah blah', 
       'X-Amz-Date': '20180518T133558Z', 
    }, 
    method: 'POST', 
    body: data 
} 
fetch('https://xxxx', aws4.sign(opts, { secretAccessKey: secretKey,  
                             accessKeyId: keyID, sessionToken: st })) 
.then(response => {res.text().then(t => console.log(t))}); 

这只是显示获取,对凭据的调用工作正常。

希望有人能帮帮我!

请您参考如下方法:

所以我想出了问题是什么。首先,这根本不是这样工作的。这样做的原因是 javascript 和原生 swift 之间的桥梁。此桥不允许传递二进制文件。在其他情况下,有一些类似在 Amazon S3 服务器上上传文件的事情,应该可以使用它们的功能,但在这种情况下,我们需要改变方式。我们现在的解决方案是从二进制转换为 base64。我们必须更改服务器上的某些内容,以便我可以通过应用程序上传 base64 字符串。 感谢所有试图帮助我的人!