1

I'm working on a quicker way to get data from Google Drive for data entry purposes. I have a bunch of folders with images in them, and I need to get the URL of each image, plus the width and height in pixels (part of the properties of each image).

I've never used the Google Drive API before, so I looked up the documentation. It has a demo app in JS that lists all files in the base directory of the drive. However, I don't know enough to change that to request the files in a certain folder.

The idea is that the user pastes in the ID of a Google Drive folder, and then the JavaScript program returns the link, width and height. The user can then simply paste that into the data entry form.

This is the function to list the files:

function listFiles() {
        gapi.client.drive.files.list({
          'pageSize': 10,
          'fields': "nextPageToken, files(id, name)",
        }).then(function(response) {
          appendPre('Files:');
          var files = response.result.files;
          if (files && files.length > 0) {
            for (var i = 0; i < files.length; i++) {
              var file = files[i];
              appendPre(file.name + ' (' + file.id + ')');
            }
          } else {
            appendPre('No files found.');
          }
        });
      }

I've seen how to do it using a URL GET request, but have no idea how to translate that into a gapi.client.drive request as shown above.

Any help here would be good.

Michael
  • 139
  • 1
  • 13

1 Answers1

1
  • In order to list the files from a certain folder, you have to provide the parameter q and specify the folder ID. It should be something like this:
'q': "'your-folder-id' in parents"

Of course, you could store 'your-folder'id' in a variable, or pass it as a parameter in your function.

  • You could also look specifically for files with certain MIME types. For example, if you wanted to look for files with MIME type image/jpeg, the parameter q should be like this:
'q': "'your-folder-id' in parents and mimeType = 'image/jpeg'"
  • Finally, if you want to retrieve the width and the height of the images, you should specify that you want to fetch the field imageMediaMetadata, like this:
'fields': "nextPageToken, files(id, name, imageMediaMetadata)"
  • So summing this up, your API call should be something along the following lines:
gapi.client.drive.files.list({
  'pageSize': 10,
  'fields': "nextPageToken, files(id, name, imageMediaMetadata)",
  'q': "'your-folder-id' in parents"
})

Reference:

I hope this is of any help.

Iamblichus
  • 18,540
  • 2
  • 11
  • 27