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
Related
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']);
}
I need the file path for a file stored in Google drive, so I can access it from a Google colab notebook.
E.g.
my_dat = ZipFile('/content/drive/MyDrive/some/file/structure/dat.zip', 'r')
Is there a quick way to "copy as path" or otherwise get the file path to the clipboard as quickly as possible from google drive?
My current method is to manually type up the file path, which is very tedious when doing it repeatedly for files deep down in directories. Basically all I need is a way to quickly and easily have 'MyDrive/some/file/structure/dat.zip' on my clipboard (e.g. similar to here), just without manually typing it.
The best way or really the only way I know of doing this using the Google drive api would be to use the
file.get method, this will return a field called parents. Once you get the parent id you can then do a file get on the parent and continue up until the parent is root. Its going to mean a lot of calls but its the only way I have found to achieve this.
I made my own library to make it easier.
!pip install kora
from kora.drive import get_path
p = get_path(file_id)
I'm trying to convert a html file (or preformatted html String) to Google Docs using drive api v3 and android studio, using these lines:
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("report.html")
.setMimeType("text/html")
.build();
(I extract the code from android-demos-master examples )
If I try put another mimetype like "application/vnd.google-apps.document", my app crash. I want to upload the file and convert to Gdocs editor or Docx. I need convert before or after upload the file. Can someone guide me?
Using the python libraries, I found I had to specify two mimetypes:
Use 'application/vnd.google-apps.document' when creating the metadata for the Drive file. This is the type of file you want created - a Google Document.
Use 'text/html' for the object representing uploaded data, as that is the type of the content. In python, this were objects of type io.MediaUpload (file upload) or io.MediaIoBaseUpload (in-memory content).
I imagine it's something similar in 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();
...
I want to change the sharing 'visibility' of currently stored documents from 'anyone with the link may view' to 'private'. This is distinct from removing named viewers and editors.
Unfortunately, the GAS has a very limited support of the documents visibibility. There is no functionality to change this option for the DocsList.File and DocumentApp.Document classes. The Spreadsheet class has the setAnonymousAccess method using which is possible to set if a spreadsheet is public.
Please open a new feature request on the issue tracker if this feature is important for you.
There is an easy way to get what you want using a method that has already been mentioned in this post
You can set the sharing / visibility parameters of any document by moving it to a shared folder. If you remove it from the shared folder then it is no long shared and that is what you wanted to do didn't you ?
So all you need to do is not to use individual sharing parameters on files but rather use the folder structure to share your files.
As a reminder, the code could be something like this to add to the folder :
function sharebyFolder(){
var file = DocsList.getFileById('docId');
var folder = DocsList.getFolderById('shared folder Id');
file.addToFolder(folder)
}
and to remove it :
function UnsharebyFolder(){
var file = DocsList.getFileById('docId');
var folder = DocsList.getFolderById('shared folder Id');
file.removeFromFolder(folder)
}
The old docs API offers a good solution:
https://developers.google.com/google-apps/documents-list/#removing_sharing_permissions