在Android到AWS S3终端节点上获取请求失败

时间:2020-05-06 22:58:46

标签: react-native amazon-s3 expo

我将React Native与Expo SDK 37一起使用。我的请求如下所示:

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上上传时出现以下错误:

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来查看是否是问题所在。徒劳无功。几乎陷入了困境,处于停滞状态。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

长时间在论坛地狱中搜寻...

在使用FormData发布数据时,Android很挑剔,对于我的两种情况,都需要将以下文件数据类型指定为image/jpgvideo/mp4

我正在使用ImagePicker中的Expo组件,因此默认情况下它返回:

{
   type: 'image' || type: 'video',
   ...other stuff
}

我解决此问题后,网络请求就无法解决。 iOS显然不需要此要求,因此这就是为什么可以在iOS上运行但不能在Android上运行的原因。