Google Drive API to search only images

Asked

Viewed 411 times

1

I recently started to study the Google Drive API. I am able to list all the files in Drive and I have access to all its attributes. However, I was asked to search only for the image files.

What did I do? I played the search code inside a Javascript:

if(file.extension === 'jpg' || file.extension === 'png')

But there it is. My search is analyzing all the files from the drive and listing only the . jpg and . png, making my search very poorly optimized. I wonder if anyone could help me. I want my search to search through Drive and return only the img extension files.

Follow the API link https://developers.google.com/drive/v3/web/quickstart/js . From now on, thank you.

  • 1

    In gapi.client.drive.files.list you can use the property q to search for files. Still in this property, you can pass the value of mimeType for certain file types. Further examples: https://developers.google.com/drive/v2/web/search-parameters

  • Could you give me an example of how to use this property q ?

1 answer

1

Since I don’t know what your code is, I’ll leave a snippet of how you can do it.

As I said in my comment, just add the property q and use one of the operators below followed by the amount you want.

Operators:

  • contains The contents of one string are present in the other.
  • = The contents of a string or boolean are the same as the other.
  • != The contents of a string or boolean are not the same as the other.
  • < The value is less than another.
  • <= The value is less than or equal to another.
  • > The reverse valve larger than another.
  • >= The value is greater than or equal to another.
  • in An element within a collection.
  • and Returns items that match both clauses.
  • or Returns items that match any clause.
  • not Denies a search clause.
  • has A collection contains an element that matches the parameters.

How to do:

"q": "<campo>" <operador> "<valor"> ["<campo>" <operador> "<valor"> ],

Example:

function listFiles() {
    gapi.client.drive.files.list({
        'pageSize': 10,
        'q': "mimeType contains 'image/jpeg' or mimeType contains 'image/png'",
        'fields': "nextPageToken, files(id, name)"
    }).then(function(response) {
        console.log('Files:');
        let files = response.result.files;

        if (files && files.length > 0) {
            for (let i = 0; i < files.length; i++) {
                let file = files[i];

                console.log(file.name + ' (' + file.id + ')');
            }
        } else {
            console.log('No files found.');
        }
    });
}
  • You don’t know how much this post helped me. It was worth it brother. Keep sowing knowledge !!!

  • Just one more question. Now I can list the type of file I want. As in the following code:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.