6

How do I append content to the end of a file in Google Drive using the API ?

Do I really have to download the whole thing, then edit the local copy, and then re-upload the whole thing again?

2 Answers 2

5

Yes you really have to download edit the file and upload it again. There is no way to programmatically edit a file. Except maybe a spreadsheet but then you would be using the Google sheets API and not the Google drive API.

Sign up to request clarification or add additional context in comments.

2 Comments

Tell me more about this google sheets API -> If I want too-> can I make a 1 cell width CSV and append that? (Thus effectively adding new lines)
Yup you can. It's an old gdata api which means it's a pain to access.
0

You can use the drive apis resumable upload with some restrictions:

  • (I don't quite remember if its true) Minimum bytes uploaded have to be 262144 except for the final upload which "creates" the file, which can contain less

  • An upload-session expires after one week, you can set Content-Range to */* if you don't know the final filesize

  • The file wont show up in google drive in the ui until complete,Upload using a similiar header to the example below, where /* is the final byte length of the file. It has to be one byte less, like in the example below: 262146/262147

I recommend getting a service account for gcp project, you can create a folder in your personal drive and share it with the service account email.

To save some time, because the drive api documentation is not the best, here in "pure" python http requests:

First you have to create the file and get the session_url:

 headers = {"Authorization": "Bearer "+myAccesstoken, "Content-Type": "application/json"} file_metadata = { 'name': "myFile.txt", 'mimeType': "text/plain", 'parents': [myFolderid], "uploadType": "resumable" } r = requests.post( "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable", headers=headers, data=json.dumps(file_metadata) ) session_url=r.headers['Location'] 

Then you can upload data to it:

 headers = { "Authorization": "Bearer "+myAccesstoken, "Content-Range": 'bytes 0-262144/*'} if is_final_data: headers = { "Authorization": "Bearer "+myAccestoken, "Content-Range": 'bytes 262144-262146/262147'} sd = io.BytesIO() sd.write(bytes("Wurst", "ASCII")) sd.seek(0) r = requests.put( session_url, headers=headers, data=sd ) 

To get last uploaded byte position, if you are resuming an upload, send an empty put request only with the session url and authorization headers and read its response headers afterwards.

You can store the session url in a file and resume upload for one week. Note: You will need something like below, since the access token is only valid for a limited amount of time.

if credentials.access_token_expired: credentials.refresh(httplib2.Http()) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.