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.
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']);
}
In the Google-Drive developers guide they show us to:
i) upload a file:
https://developers.google.com/drive/manage-uploads
ii) update a file's data:
https://developers.google.com/drive/v2/reference/files/update
The second option ii) update a file data by overwriting the file's previous data. Is there anyway I can add data to a file without overwriting it? Just add lines to a file?
Thanks.
No
The Drive API deals with files as a whole.
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();
...
It's possible to rename the file while uploading to my account in box.net?
I'm using the 1.0 version and the next url:
https://upload.box.net/api/1.0/upload/myauth/myfolderid?share=1
I know it's possible to rename the file using ajax but there's nothing in the API, a option like "share" for example, to do that?
There isn't a way to rename the file and upload in the same API call in V1, but you can rename the file with a second API call: http://developers.box.net/w/page/12923947/ApiFunction_rename
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