Uploading zero size files to google drive - google-drive-api

I am using gdata python client for uploading/downloading files. I am not able to figure out how to upload zero sized files. Here is what I have.
#To update
file = gdata.data.MediaSource()
r = client.GetResourceById(resid)
client.UpdateResource(r, media=file, new_revision=True)
#To create a new file
file = gdata.data.MediaSource()
doc = gdata.docs.data.Resource(type='file', title="Title")
r = client.CreateResource(doc, create_uri=uri, media=file)
I tried with removing media=None and also not including in the options. How can I make it work for zero sized files?

You can use the snippet for the files.insert method of the Drive API and not specify a media_body to create an empty file:
https://developers.google.com/drive/v2/reference/files/insert

Related

Is there a way to use Google App Script to create a Drive file sharable in view-only but that can't be re-shared or downloaded?

I have just recently starting coding and using AppScrpits, and I have set up an app that among other things creates PDF files in a Drive folder and stores the view links in a Sheet.
These files should be accesible only by people with the same domain in view only, non-sharable and non-downloadable.
I set the access and permission as intenden and everything works fine, but the PDF files are still downloadable and sharable by the viewers. Normally I would set this option by right cliking the file and setting the configuration manually, but since I have to process a large number of files I wonder if there is anyway to do this with appscripts.
With the code below access and view permission works as intended, but I can't find any referance to set aditional configuration so that the file is non-downloable or sharable.
let pdf = DriveApp.getFileById(nuevo.getId())
.getBlob()
.getAs('application/pdf');
pdf.setName('placeholder'.pdf');
let file = ubicacion.createFile(pdf);
file.setSharing(DriveApp.Access.DOMAIN_WITH_LINK, DriveApp.Permission.VIEW);
var ultimaFila = sheet.getLastRow() +1;
sheet.getRange('Hoja 1!B' + ultimaFila).setValue(file.getUrl());
Thanks!!!

GAS - upload file to drive with version

I can use GAS script to upload a file to my drive as below:
media = MediaFileUpload(
filename,
mimetype=mimetype,
resumable=True
)
request = service.files().create(
media_body=media,
body={'name': remotefilename, 'parents': [folderId]}
)
But if I upload the same file multiple times, it will generate multiple copies. Is it possible to keep the same file but keep different version? just like what the UI update version does?
Solution:
Instead of a create request, you can issue an update request to an existing file with newRevision parameter set to true to create a new revision for the same file.
For more information on how to use Files: update, you can check the official API documentation.

Finding a .txt file on Google Drive by name and getting its contents

I want to find text files by their name and then get their contents and make them into a var.
I have tried to find the file by its name, but it doesn't seem to work. I'm clueless as to how to find the file contents though.
My code to find the file:
function testThing() {
var findquestions = DriveApp.getFilesByName('tempquestions.txt')
Logger.log(findquestions)
}
I want it to log what it found, but the output is nothing but: "FileIterator". I don't know what that means.
As you can see in the documentation, .getFilesByName return fileiterator. What's a file iterator? The documentation states
An iterator that allows scripts to iterate over a potentially large collection of files
There may be large amount of files with the same name. This iterator provides access to all those files.
What methods provide access to file from fileIterator? This method does.
How to get contents of such file? Get blob from file and getDataAsString from blob
Logger.log(DriveApp
.getFilesByName('tempquestions.txt') //fileIterator
.next() //file(first file with that name)
.getBlob() //blob
.getDataAsString() //string
)

Need to convert html to google doc native format using Java

I've read through the API documentation at https://developers.google.com/drive/v2/reference/ however I cannot find the answer to my question. And attempts to google a solution have failed.
I have a series of previously uploaded small HTML files sitting in Google Drive. What I want to do is write a short application to convert each of these to native Google Document format (mime type "application/vnd.google-apps.document").
I want to do this using Java code and not using GAS code.
The approach I used was to query drive for the File object corresponding to the item I want to convert. Then I pull that file's content as a string. Then I create a new file of mime type "application/vnd.google-apps.document" and upload it with the HTML content. Not surprisingly it didn't work.
So then I tried a different approach: Upload the content as "text/html" but set the "convert" flag to "true". Well I didn't see any direct API to set the convert flag to true. So I tried:
File oBody = new File() ;
oBody.setTitle ( sTitle ) ;
oBody.setDescription ( sDescription ) ;
oBody.setMimeType ( sMimeType ) ;
oBody.set("convert", bConvert);
This did not fail. But it did not create a Google Document either. It just created a text file identical to the original file.
How do I upload a document containing "text/html" content and get Google Drive to convert it automatically to a Google Document?
The convert flag has to be set in the files.insert request and not the File resource.
Using the snippet in the files.insert documentation as reference, this is what you should do:
...
File file = service.files().insert(body, mediaContent).setConvert(true).execute();
...

Copy document contents in google drive api

I need to copy the content of a document into another, both stored in google drive, using Google Drive Api for Java. I'm able to upload or download documents but I don't know how directly transfer the content of a document into another one. I thought of something like that:
public void copyContent(String sourceId, String destinationId) {
File sourceFile = service.files().get(sourceId).execute();
AbstractInputStreamContent content = null; //sourceFile.getContent()??? :-(
File destinationFile = service.files().get(destinationId).execute();
service.files().update(destinationId, destinationFile, content).execute();
}
There's a way to get the file content as AbstractInputStreamContent? Or maybe it exists another approach to copy contents? I need a solution without exporting / uploading documents because I noticed some conversion problem. Thanks!
You can use the files.copy method of the Drive API to create a copy of an existing document:
https://developers.google.com/drive/v2/reference/files/copy