在 Android 上获取请求失败到 AWS S3 端点 [英] Fetch requests failing on Android to AWS S3 endpoint

查看:21
本文介绍了在 Android 上获取请求失败到 AWS S3 端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 Expo SDK 37 的 React Native.我的请求如下所示:

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_moduleswhatwg-fetchdistfetch.umd.js:473:29 in xhr.onerror
- node_modulesevent-target-shimdistevent-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules
eact-nativeLibrariesNetworkXMLHttpRequest.js:574:29 in setReadyState
- node_modules
eact-nativeLibrariesNetworkXMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules
eact-nativeLibrariesvendoremitterEventEmitter.js:190:12 in emit
- node_modules
eact-nativeLibrariesBatchedBridgeMessageQueue.js:436:47 in __callFunction
- node_modules
eact-nativeLibrariesBatchedBridgeMessageQueue.js:111:26 in __guard$argument_0
- node_modules
eact-nativeLibrariesBatchedBridgeMessageQueue.js:384:10 in __guard
- node_modules
eact-nativeLibrariesBatchedBridgeMessageQueue.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/jpgvideo/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆