Skip to content

Instantly share code, notes, and snippets.

@wit-ayush
Last active August 12, 2022 12:12
Show Gist options
  • Save wit-ayush/629d7b29d9aed4b62c473482fdc80241 to your computer and use it in GitHub Desktop.
Save wit-ayush/629d7b29d9aed4b62c473482fdc80241 to your computer and use it in GitHub Desktop.
// storing video in rom using RNFS (import RNFS from 'react-native-fs';)
let outputVideoPath = `${RNFS.DocumentDirectoryPath}/${fileDest}.mp4`;
// upload func.
const uploadMediaToS3 = async (filePath: any, fileName: string, contentType: any, folderPath: string, Token: any ) => {
var s3folderpath = contentType.split('/')[0] == 'video' ? '' :`${folderPath}`
var bodyFormData = new FormData();
var photo: any = {
uri: filePath,
type: contentType,
name: fileName,
};
bodyFormData.append('folderPath', s3folderpath);
bodyFormData.append('file', photo );
const data = await axios({
method: "post",
url: `${URL}/upload_media`,
data: bodyFormData,
headers: {
"Content-Type": "multipart/form-data",
'Authorization': `${Token}`
},
})
if(data.data.url ){
if(contentType.split('/')[0] == 'video'){
// deleting file from rom after uplaod
RNFS.unlink(filePath)
.then(() => {
console.log('VIDEO FILE DELETED');
})
.catch((err) => {
console.log('VIDEO FILE DELETED FAILED');
console.log(err.message);
});
}
return data.data.url
}
else{
return "error"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment