在Android到AWS S3终端节点上获取请求失败 [英] Fetch requests failing on Android to AWS S3 endpoint
问题描述
我将React Native与Expo SDK 37一起使用.我的请求如下所示:
I am using React Native with Expo SDK 37. My request looks like the following:
export const uploadMedia = async (fileData, s3Data) => {
console.log(fileData.type)
let formData = new FormData();
formData.append('key', s3Data.s3Key);
formData.append('Content-Type', fileData.type);
formData.append('AWSAccessKeyId', s3Data.awsAccessKey);
formData.append('acl', 'public-read');
formData.append('policy', s3Data.s3Policy);
formData.append('signature', s3Data.s3Signature);
formData.append('file', fileData.data);
return fetch(`https://...restofendpoint`, {
method: 'POST',
//skipAuthorization: true,
body: formData
})
}
我正在使用预签名的POST方法.我从服务器上的另一个端点获取以下s3数据,并且可以确认是否已填充所有正确的信息.这在iOS上完美运行,但是当尝试在Android上上传时,出现以下错误:
I am using presigned POST method. I get the following s3 data from another endpoint on our server and can confirm all the correct info is being populated. This works perfectly on iOS but when attempting to upload on Android I get the following error:
Network request failed
- node_modules\whatwg-fetch\dist\fetch.umd.js:473:29 in xhr.onerror
- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:574:29 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue
我最初使用axios
接收了所有请求,并尝试通过切换到fetch
api来查看是否是问题所在.徒劳无功.几乎陷入了困境,处于停滞状态.任何帮助将不胜感激.
I originally had all my requests using axios
and tried to see if that was the issue by switching to fetch
api. No avail. Pretty much stumped and at a standstill. Any help would be appreciated.
推荐答案
长时间在论坛地狱中搜寻...
After long searching in the depths of forum hell...
使用FormData
发布数据时,Android很挑剔,对于我的两种情况,都需要将以下文件数据类型指定为image/jpg
或video/mp4
.
When posting data using FormData
Android is picky and needs the following the file data type to be specified as image/jpg
or video/mp4
for both my cases.
我使用的是Expo
中的ImagePicker
组件,因此默认情况下会返回:
I was using the ImagePicker
component from Expo
so it by default returns:
{
type: 'image' || type: 'video',
...other stuff
}
一旦我解决此问题,网络请求就无法解决. iOS显然不需要此要求,因此这就是为什么可以在iOS上运行但不能在Android上运行的原因.
The network request failed resolved as soon as I fixed this issue. iOS apparently doesn't need this requirement so that is why is was working on iOS but not Android.
这篇关于在Android到AWS S3终端节点上获取请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!