I working on a google drive project but I am not able to send search parameters with Axios to google API from frontend react
can any explain to me how to write queries like in google drive api url
- Search all files and folders by date
- Find a file by size
- Find all empty folders
- Find a file by type such as ppt, image, etc
Axios request look like that
axios.get('='+accessToken+'&q=mimeType%3D%22application%2Fvnd.google-apps.folder%22').then(res=>{
console.log(res)
setFolders(res.data.files)
}).then(err=>{
console.log(err)
})
Thanks in advance :)
I working on a google drive project but I am not able to send search parameters with Axios to google API from frontend react
can any explain to me how to write queries like in google drive api url
- Search all files and folders by date
- Find a file by size
- Find all empty folders
- Find a file by type such as ppt, image, etc
Axios request look like that
axios.get('https://www.googleapis./drive/v3/files?access_token='+accessToken+'&q=mimeType%3D%22application%2Fvnd.google-apps.folder%22').then(res=>{
console.log(res)
setFolders(res.data.files)
}).then(err=>{
console.log(err)
})
Thanks in advance :)
Share Improve this question edited Feb 15, 2022 at 9:01 Air University asked Feb 15, 2022 at 8:45 Air UniversityAir University 851 silver badge8 bronze badges 8- do you get any errors? – MohammadHossein Shafiei Commented Feb 15, 2022 at 8:47
- yes this api request show error ? – Air University Commented Feb 15, 2022 at 8:49
- kindly show me how to send a proper axios request – Air University Commented Feb 15, 2022 at 8:50
- 1 The drive API documentation is very exhaustive with node.js examples. developers.google./drive/api/v3 S.O. is not a place to ask other to code for you but to ask how to solve error you're facing. In this particular case please show us how you tried to achieve at least the first problematic and people may then be able to help you. – Ivo Commented Feb 15, 2022 at 8:52
- I just want to know how to write queries – Air University Commented Feb 15, 2022 at 8:57
2 Answers
Reset to default 5This is how you can use the Google Drive API with axios for each of your questions:
- Search all files and folders by date
Yes you can! you can use createdTime and modifiedTime filters (see more available query terms here)
- Find a file by size
Yes, you can, but, you will need to return the size of the file/folder and filter the resulting files by specific size, you don't need to download the file, but it could be a little inefficient since you need to fetch the files first (use query filters to limit the results).
- Find all empty folders
Yes, you can! but similar to the previous point, you need to iterate each folder and search files using the folder id, checking if files are empty. Ensure you're filtering folders only by using mimeType filter application/vnd.google-apps.folder
- Find a file by type such as ppt, image, etc
Use mimeType i.e image/jpeg
Google drive search filters | View in Fusebit |
---|
// Search all files and folders by date
const dateFilter = new Date('January 01, 2022').toISOString();
// 1. Search all files and folders by date
const filesFilteredByDate = await axios.get('https://www.googleapis./drive/v3/files', {
params: {
q: `createdTime >= '${dateFilter}' or modifiedTime >= '${dateFilter}'`,
fields: 'files(id,name,modifiedTime,createdTime,mimeType,size)',
spaces: 'drive',
},
headers: {
authorization: `Bearer ${access_token}`
}
});
// 2. Find a file by size
const sizeInBytes = 1024;
const filesFilteredBySize = filesFilteredByDate.data.files.filter(file => Number(file.size || 0) >= sizeInBytes);
// 3. Find all empty folders
const emptyFoldersSearch = await axios.get('https://www.googleapis./drive/v3/files', {
params: {
q: `mimeType = 'application/vnd.google-apps.folder'`,
fields: 'files(id, name)',
spaces: 'drive',
},
headers: {
authorization: `Bearer ${access_token}`
}
});
const emptyFolders = [];
for await (const folder of emptyFoldersSearch.data.files) {
const childrenResponse = await axios.get('https://www.googleapis./drive/v3/files', {
params: {
folderId: folder.id,
spaces: 'drive',
},
headers: {
authorization: `Bearer ${googleClient.fusebit.credentials.access_token}`
}
});
if (!childrenResponse.data.files.length) {
emptyFolders.push(folder);
}
}
// 4. Find a file by type such as ppt, image, etc
const mimeType = 'image/jpeg';
const filesFilteredByType = await axios.get('https://www.googleapis./drive/v3/files', {
params:{
q: `mimeType:'${mimeType}'`,
fields: 'files(id,name,mimeType,size)',
spaces: 'drive',
},
headers: {
authorization: `Bearer ${access_token}`
}
});
console.log(`Found ${filesFilteredByDate.data.files.length} files/folders created or modified at ${dateFilter}`);
console.log(`Files larger than ${sizeInBytes} bytes:`, filesFilteredBySize);
console.log(`Found ${emptyFolders.length} empty folders`);
console.log(`Found ${filesFilteredByType.data.files.length} images of type ${mimeType}'`);
Axios is not your problem the API does not support what you are trying to do for the most part.
Search all files and folders by date
You cant really. Download them all and search locally
Find a file by size
You cant. Download them all and search locally
Find all empty folders
Its not easy and will be a lot of requests but. You could use q to only get folders and then make separate requests using parents and then see if there are any files returned.
Find a file by type such as ppt, image, etc
Us the q parameter and search by mime type you will just need to google the correct mime type for the file you are looking for.
mimeType='application/vnd.ms-powerpoint'
You should have a look at the Search terms refrence documentation it will show you wnat you can search on.