I am going to upload image on Amazon S3 in React Native.
I have S3 signedUrl for uploading image.
How can I upload image using this url in React Native?
If anybody tried this, please help me.
Thank you!
I am going to upload image on Amazon S3 in React Native.
I have S3 signedUrl for uploading image.
How can I upload image using this url in React Native?
If anybody tried this, please help me.
Thank you!
It's quite simple, I spent a lot of time to discover that:
const xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
// success
} else {
// failure
}
}
}
xhr.open('PUT', presignedUrl)
xhr.setRequestHeader('Content-Type', fileType)
xhr.send({ uri: filePath, type: fileType, name: fileName })
source: http://blog.rudikovac.com/react-native-upload-any-file-to-s3-with-a-presigned-url/
You can just use fetch
as following:
import { ImagePickerIOS } from 'react-native';
ImagePickerIOS.openSelectDialog({},(assetUri)=>{
var photo = {
uri: assetUri,
type: 'image/jpeg',
name: 'main.jpg'
}
var body = new FormData();
body.append('file', photo);
fetch(presignedUrl, {
method: 'put', body: body
});
},()=>{ console.log('failed'); })
const file = {
uri: response.uri,
name: "image.png",
type: "image/png"
}
const options = {
keyPrefix: "",
bucket: "",
region: "",
accessKey: "",
secretKey: "",
successActionStatus: 200
}
RNS3.put(file, options).then(response => {
if (response.status !== 200)
alert(response.body);
else {
//Success##
----------
##
alert(JSON.stringify(response))
}
});