I was facing the same issue. I was trying to upload images from the phone's camera roll or photo gallery to AWS S3. It was working fine for IOS, but was throwing error TypeError: Network request failed for Android device.
So, I used npm library rn-fetch-blob, instead of fetch() method.
import RNFetchBlob from 'rn-fetch-blob';
RNFetchBlob.fetch('PUT', AWSPresignedURL, {'Content-Type': 'multipart/form-data'}, Platform.OS === 'ios' ? RNFetchBlob.wrap(filePath) : `RNFetchBlob-${filePath}`)
Please note, in my case, I had different file paths for IOS and Android devices. We handle it in different ways using rn-fetch-blob
Sample filePath variable data for
- IOS device -
"/Users/Niveditha/Library/Developer/CoreSimulator/Devices/B41EB910-F22B-4236-8286-E6BA3EA75C70/data/Containers/Data/Application/B88777C6-6B10-4095-AB67-BB11E045C1DE/tmp/react-native-image-crop-picker/img.jpg"
- Android device -
"file:///storage/emulated/0/Android/data/com.uploadcameraroll/files/Pictures/img.jpg"
Code to generate AWSPreSignedURL -
let AWS = require('aws-sdk');
let S3 = new AWS.S3();
AWS.config.region = 'ap-south-1';
AWS.config.accessKeyId = 'AKXXXXXXXXXXXXXXXX';
AWS.config.secretAccessKey = 'XIJyXXXXXXXXXXXXXXXXXXXXXXXXXXX+H+GR';
S3.getSignedUrl( 'putObject' ,
{ Bucket: 'upload-app-photos',
Key: 'upload/' + fileName,
Expires: 120,
ACL: 'public-read'
}, (err,url) => {
if (err) {
console.log("error ", err);
return;
}
console.log("AWSPreSignedURL ", url);
})
This issue was officially raised - https://github.com/facebook/react-native/issues/25244
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…