Home > database >  Google drive API - Getting name of folder from id
Google drive API - Getting name of folder from id

Time:03-15

I'm making an app using nodejs where I need to download files from folders on Google drive. This is the code I use for downloading the files inside a given folder link. folderId is the id from the shared link obtained from google drive.

function DlImgFromFolder(auth, folderId, folderName) {
  const drive = google.drive({version: 'v3', auth});
  var query = "'"   folderId   "' in parents and mimeType contains 'image/' and trashed = false";
  drive.files.list({
    q: query,
    fields: 'files(id, name)',
  }, function(error, response) {
    if (error) { return console.log("ERROR", error); }
  
    response.data.files.forEach(function(item) {
      var file = fs.createWriteStream("./"  folderName  '/'  item.name);
      file.on("finish", function() {
        console.log("downloaded", item.name);
      });
  
      // Download file
      const drive = google.drive({version: 'v3', auth});
      drive.files.get({
            fileId: item.id,
            alt: 'media'
        }, {
            responseType: 'stream'
        },function(err, response){
            if(err)return '';
            
            response.data.on('error', err => {
                
            }).on('end', ()=>{
                
            })
            .pipe(file);
       });
      


    });
  });
}

So far I'm manually assigning the folder name but I'd like to automatically name it based on the folder's original name. I have read the api docs and searched on google but couldn't find anything about this. Is there a way to get the name of the folder based on the url/id provided for download?

CodePudding user response:

In your script, how about the following modification?

Modified script:

async function DlImgFromFolder(auth, folderId) {
  const drive = google.drive({ version: "v3", auth });
  const res = await drive.files
    .get({ fileId: folderId })
    .catch((err) => console.log(err.errors));
  if (!res) return;
  const folderName = res.data.name;

  var query =
    "'"  
    folderId  
    "' in parents and mimeType contains 'image/' and trashed = false";
  drive.files.list(
    {
      q: query,
      fields: "files(id, name)",
    },
    function (error, response) {
      if (error) {
        return console.log("ERROR", error);
      }

      response.data.files.forEach(function (item) {
        var file = fs.createWriteStream("./"   folderName   "/"   item.name);
        file.on("finish", function () {
          console.log("downloaded", item.name);
        });

        // Download file
        drive.files.get(
          {
            fileId: item.id,
            alt: "media",
          },
          {
            responseType: "stream",
          },
          function (err, response) {
            if (err) return "";

            response.data
              .on("error", (err) => {})
              .on("end", () => {})
              .pipe(file);
          }
        );
      });
    }
  );
}
  • In this modification, the folder name is retrieved from the inputted folder ID using "Files: get" method.

Note:

  • In this case, when your local PC has no folder of folderName, an error occurs. Please be careful about this.

Reference:

CodePudding user response:

I notice you are using the:

files.list

With that one you can only use it to get the parents information unless you use it in the query, so you can only find folders and would include the name of the folder:

mimeType='application/vnd.google-apps.folder'

You can test it and over the documentation from above and for the fields just add "*".

Each file resource in the response will have a property parents which would be an ID for the folder where the file is located. You can use this folder ID with a files.get request to check if it is the folder you are looking for, you can test it here. You can place in the "fields" parameter just "name".

  • Related