react-native-cloud-fs
react-native-cloud-fs copied to clipboard
How to get the files from Google Drive?
Get the file or selected file path from google drive. state any example for that?
my code:
const sourceUri = {uri: 'https://drive.google.com'};
const destinationPath = "foo-bar/docs/info.pdf";
const mimeType = null;
const scope = 'visible';
RNCloudFs.copyToCloud({
sourcePath: sourceUri,
targetPath: destinationPath,
mimeType: mimeType,
scope: scope
})
.then((path) => {
console.log("it worked", path);
})
.catch((err) => {
console.warn("it failed", err);
})
Just in case somebody still need it: i'm using react-native-google-drive-api-wrapper and react-native-fs
After initializing Gdrive (see google-drive-api-wrapper docs) you need to get the folderId, just in case you placed your file in any folder
let folderId = await GDrive.files.safeCreateFolder({
name: "MyFolder",
parents: ["root"]
})
then, use Gdrive's getId to find your file id
let myFileId= await GDrive.files.getId(
`myFileName`,
[folderId ],
)
Make sure the folder where you want to place your file exists with RNFS
await RNFS.exists(`${RNFS.DocumentDirectoryPath}/myFolder/`).then(async exists => {
if (!exists) {
await RNFS.mkdir(`${RNFS.DocumentDirectoryPath}/myFolder/`)
}
})
Finally, you can download your data:
GDrive.files.download(fileId, { toFile: `${RNFS.DocumentDirectoryPath}/myFolder/filename.json` }, { alt: "media" }).promise.then(async (res) => {
if (res.statusCode == 200) {
myFileContent = await RNFS.readFile(${RNFS.DocumentDirectoryPath}/myFolder/filename.json, 'utf8')
console.log(myFileContent)
} else {
throw new Error("Error")
}
})