Search code examples
node.jsgoogle-apps-scriptgoogle-apigoogle-oauthgoogle-api-client

How to make all files accessible in a single Google Drive folder?


I am trying to access media files in a folder that is created by my webapp using the official google api library for node.js (https://github.com/googleapis/google-api-nodejs-client/).

On successful oauth, I create a folder MyAppFolder, and a media folder inside it.

The idea is that users will fill this media folder with whatever photos and videos they want, then my webapp will take them and display them on a page for the user to get an aggregate view of their media. I am able to get all the media files within the media folder. Here is my snippet of code:

async function getGoogleDriveMedia({ user, credentials }) {
  // await prepareDrive({ user, credentials });
  const rootFolder = await getRootFolder({ user, credentials });

  if (!rootFolder) {
    return;
  }

  const client = await createOauthClient({ user, credentials });
  const drive = google.drive({
    version: 'v3',
    auth: client,
  });
  const mediaFolderRes = await drive.files.list({
    q: `mimeType='application/vnd.google-apps.folder' and name='media' and '${
      rootFolder.id
    }' in parents`,
    fields: 'nextPageToken, files(id, name, parents)',
  });

  const mediaFolder = _.get(mediaFolderRes, 'data.files[0]');
  if (!mediaFolder) {
    return;
  }

  const mediaRes = await drive.files.list({
    q: `'${
      mediaFolder.id
    }' in parents and (mimeType contains 'image/' or mimeType contains 'video/')`,
    fields:
      'nextPageToken, files(' +
      [
        'id',
        'name',
        'webContentLink',
        'webViewLink',
      ].join(', ') +
      ')',
  });

  return _.get(mediaRes, 'data.files');
}

The problem now is that I'm not able to display these media because they are not publicly accessible. Is it possible to make the MyAppFolder and everything within it accessible to the public with a single permissions update? Or do I need to do it per file?

I also did a check on the fields.files parameter on their API explorer: https://developers.google.com/apis-explorer/#search/drive.files.list/m/drive/v3/drive.files.list

There isn't something like a .previewLink or some other image URL field.

How can I show these images on my webapp?


Solution

  • Is it possible to make the MyAppFolder and everything within it accessible to the public with a single permissions update? Or do I need to do it per file?

    No there is no way to update everything in a file at once. You are going to have to update each file. Even updating the folder will not change the settings for the files with in the folder.

    Just have your application change the permissions on the file after its uploaded. In the mean time run though all the existing files and change their permissions.

    Note: I hope you have informed your users that their files will be made public.