Download folder with Google Drive API - google-drive-api

I have some data on Google Drive, organized in folders, which I want to propagate on other servers. I have some script for propagating, but I need to download data from google drive. Is there a method for downloading folders via Google Drive API, that is also maintaining whole folder structure?

Folders are also files on Google Drive. The only difference is the Mime type. with folders its mimeType = 'application/vnd.google-apps.folder'.
There is no single method that will allow you to download everything with in a folder. Your going to have to do a file.list searching for the files with the parent Id to the file Id of your parent folder using search parameters (TIP: ''1234567' in parents'). This will return a list of the files contained within your folder. Then download each one.
Update from comment you need to loop though each directory or just do a main list of everything on your drive account and process the data locally.
File 1 (folder)
----> File 2 (folder )
--------> File 3 (actually a file)
---->File Four (actually a file)
'File 1' in parents
returns everything within the file 1 directory. If the mime type of the item returned is a directory (mimeType = 'application/vnd.google-apps.folder') make a request to get its contences
'File 2' in parents
returns everything within the file 2 directory.

FileList result = service.files().list().setQ("parents='1NzSAZEwAFARWegk42ANrWQrTopWQTdGB'").setSpaces("drive")
.setFields("nextPageToken, files(id, name)")
.setPageToken(pageToken)
.execute();
The parents parameter is the the fileId of the folder you want to download files from. Doing this you can get all files in that particular folder.

Related

Is there a way to find md5sum for Google drive uploads?

I have to upload some large files and folders to Google Drive. In order to make sure data is uploaded perfectly, I need to check their md5sums. Is there any other way to check uploaded files are completely uploaded?
When you upload the file the file resource is returned to you. The file resource contains a md5Checksum you should just be able to compare it with the one you have.
Try a file.list and you can see them all.
let files = Drive.Files.list();
for (item of files.items) {
Logger.log(item['md5Checksum']);
}

How to delete a file (deep) in a ZIP folder in Python?

I'm writing a script that is extracting 2 JSON files from deep (3 folders down) in a ZIP Folder, modifying its contents, and writing those files back into that same folder. The only issue is that when I write the JSON Files back into the folder where they were initially pulled from, they do not overwrite so now I have 4 files named (product, product, package, package). I wish to delete or overwrite the non-modified files but don't know how.
I have looked up how to do it on other threads and it says it's not supported but I was hoping by now someone had addressed this without having to do the "rewrite the entire directory minus the old files into a new ZIP" routine. I've also tried writing to the zip file with 'w' mode but that just deletes the whole other contents of the zip file.
This is how I'm writing each file back into the ZIP file
zf = zipfile.ZipFile(ZipName, 'a')
zf.write('package.json',addressBook[0],compress_type = zipfile.ZIP_DEFLATED
zf.write('product.json',addressBook[1],compress_type = zipfile.ZIP_DEFLATED

Can copy file, can't add it Google script

In a Google script, I have a folder object (testFolder) and a file object (testFile). I have these two lines of code:
testFolder.addFile(testFile);
testFile.makeCopy('this is a copy', testFolder);
The second line correctly copies the file into the folder.
The first line seems to do nothing. I'm expecting it to add a reference to the file and place it in the folder.
I obviously have the correct objects and I am the owner of the file and the folder, so any other ideas?
Thanks
You can make a copy create new file in Google Drive. But one cannot simply move a file into a folder. You have to make a copy into the desired folder and then remove the old file. Or you have to directly create the file in the desired folder location.
What is your code for getting the 'testFile' and 'testFolder' objects? Are you referencing them by ID or by name? If you have multiple folders of the same name in your Google Drive, this could be causing the issue. Here's the script that worked for me
var file = DriveApp.getFileById('YOUR_FILE_ID');
var folder1 = DriveApp.getFolderById("YOUR_FOLDER1_ID");
var folder2 = DriveApp.getFolderById("YOUR_FOLDER2_ID");
file.makeCopy('another copy', folder1);
folder1.addFile(file);

Embedding folders in a Google File Cabinet and including files in the folders

Refering to the link and the section 'Embedding folders in a File Cabinet' I've used this script to control and update a file cabinet in my google sites page. However, it only works on the root folder and doesn't add files that are in the sub folders. Please could you help me understand how you pick this up and also control the file cabinet so that it displays this easily?
This is the current appscript code:
function showFolderInSite() {
//var files = DocsList.getFolder("xxxxxxx").getFiles(); commented out this line as using getfolderbyID.
var files = DocsList.getFolderById('xxxxxxxxxxxxx').getFiles();
var page = SitesApp.getPageByUrl('https://sites.google.com/x/xxxxxxxx/xxxxx/xxxxxxxxx');
var attachments = page.getAttachments();
for (i in attachments) {
attachments[i].deleteAttachment();
}
for (i in files) {
page.addWebAttachment(files[i].getName(), '', files[i].getUrl());
}
}
I just tried to attach a picture but apparently I need some reputation points to post images.. how do I get those?! this is the link to it incase it does work <> In the meantime the picture shows the folders have been added but there are no files when you expand them.
Edit 5/8/14: No replies yet (hope someone can help soon). Some ideas of mine... Do I need to track what is a file and what is a folder and then assume that when the array is populated what comes through after a folder will be what is found in that folder. One problem with that is how do you differentiate a folder in a sub folder.
var files = DocsList.getFolderById('xxxxxxxxxxxxx').getFiles();
This row returns all the files of the first level in the folder. It does not retrieve any subfolders or their files. This is the same for DriveApp note that Docslist is deprecated
So getfiles() from DriveApp:
Gets a collection of all files that are children of the current
folder.
This means that you need to recursively fetch all the subfolders and their files. Also note that folders are also files in Google Drive, they only have a different mime type, application/vnd.google-apps.file
Your current script fetches the files, including the folders of the current folder, and creates links to those files and folders. It does not upload those files into the file cabinet, only refers to them. If that is the goal, you only need to recurse into the subfolders. Here you also need to note that file cabinets only go one level deep, you will not be able to create a similar structure using the file cabinet if the folders are more than one level deep.

When adding a file to a folder, how to make it not default save in Google Drive?

In my script I have template docs in the main drive, but when the copy is created and populated with my excel data, I want it to be saved in a folder in drive. I have the script do that, but it has one copy of the file in the Main Google Drive, and the other in the folder I want it in. If I delete one of the copies, it deletes both.
Is there any way I can have it save automatically in the specified folder without also being in the main drive folder?
Folders in Google drive are not exactly like forders in a computer : having the file in your 'root' folder and in another folder doesn't mean there are 2 files, but rather 1 and only file with 2 labels... that's why you can't delete one without deleting the other !
The solution is simply to play with these labels in the script, here is how it works : (I commented each step to make it clear.)
function othertest(){
folder=DocsList.createFolder("MyFolder"); // or getFolderById or whatever other way to get your target folder
var file=DocsList.createFile('File2', 'Empty');// just an empty file for test but this would be your file copy that you want to "move"
file.addToFolder(folder);// put it in the folder
file.removeFromFolder(DocsList.getRootFolder());// and remove from the root
}
The other possible solution is to create the file directly in the target folder since the folder object supports the createFile method. (Not sure though that you can do it in your specific use case)
here is an example, you can see that the file is not in the root folder.
function createFileinFoldertest() {
var folder = DocsList.getFolder('test')
folder.createFile('Empty test fileName','nothing in there')
}