Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing objects from Google Drive #92

Open
marksandford opened this issue Oct 17, 2018 · 2 comments
Open

Importing objects from Google Drive #92

marksandford opened this issue Oct 17, 2018 · 2 comments

Comments

@marksandford
Copy link

IMI can pull objects directly from a Google Drive with the following caveats/notes:

  1. The URI must be constructed using an API key to provide a direct download link, since shareable link points to a viewer, not the object.
  2. Files must be shared correctly.
  3. Google Team Drive does not allow sharing on folder level, only on a file by file basis.

If it's useful, below is a Google Code snippet intended to be run in a Google Sheet that will search through a GDrive folder and generate download links which the IMI can use to grab the objects. Commented out is also a line that will change permissions one at a time, only necessary for Team Drive (Insert obvious scary language about allowing that level of access to a script). Changing permissions, even via the API, can take a long time especially on a directory with a large number of files in it. It's better to change permissions on the folder level when possible (eg not in Team Drive).

 function listFilesInFolder(folderName) {

   
   var ui = SpreadsheetApp.getUi();
   var response = ui.prompt('Required', 'Enter Folder ID', ui.ButtonSet.OK_CANCEL);     
   var sheet = SpreadsheetApp.getActiveSheet();
   sheet.appendRow(["Name", "File-Id"]);
   var folder = DriveApp.getFolderById(response.getResponseText());
    var contents = folder.getFiles();

    var cnt = 0;
    var file;

    while (contents.hasNext()) {
        var file = contents.next();
        cnt++;
           // file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
           // Replace {GOOGLEAPIKEY} in the following line with your api key.
           // Instructions for getting API key: https://support.google.com/googleapi/answer/6158862?hl=en
           var fileURL = "https://www.googleapis.com/drive/v3/files/" + file.getId() + "/?key={GOOGLEAPIKEY}&alt=media";
           data = [
                file.getName(),
                fileURL,
            ];

            sheet.appendRow(data);
    };
};
'''
@DiegoPino
Copy link
Contributor

DiegoPino commented Oct 17, 2018 via email

@DiegoPino
Copy link
Contributor

@marksandford where do you feel this script/option should live in? Docs? Its own chapter? maybe a cookbook folder?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants