Reputation: 85
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
Axios request look like that
axios.get('https://www.googleapis.com/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 :)
Upvotes: 4
Views: 2647
Reputation: 1206
This is how you can use the Google Drive API with axios for each of your questions:
Yes you can! you can use createdTime and modifiedTime filters (see more available query terms here)
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).
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
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.com/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.com/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.com/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.com/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}'`);
Upvotes: 5
Reputation: 117016
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.
Upvotes: 2