How to connect a resuming download to Google Drive using google-api-ruby client?

I'm having trouble downloading large files from my Linux desktop to GDrive (using Grive, which does not offer recovery or even monitoring of large downloads).

So, I came up with using google-api-ruby to write a small script to complete the task.

As the google docs documentation talks about renewable downloads - https://developers.google.com/drive/manage-uploads#resumable - I tried to implement renewable downloads using google-api-ruby and found Google::APIClient::ResumableUpload class .

Samples are very useful for understanding how this ruby ​​API works, but the samples do not use renewable downloads.

Is Google::APIClient::ResumableUpload the ment class that will be used for the Google::APIClient::ResumableUpload downloads mentioned in the document, or rather, the convexity class provided by this Ruby client?

Constructor

Google::APIClient::ResumableUpload consumes Google::APIClient::Result , which I get when I call execute or execute! on the Google::APIClient instance Google::APIClient .

As far as I understand, the execute method will only work with Google API methods that are available for discovery APIs (e.g. drive.files.insert ).

Unhappiness Renewable downloads are tied to the following URI:

https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable

and it seems that it is not yet part of Discovery or integrated into a β€œcleaner” URI scheme (why upload/drive instead of drive/upload ?).

Is it possible to use Google::APIClient for this URI API to combine it with Google::APIClient::ResumableUpload , or do I need to implement Resumable uploads myself (for example, using em-http-request )?

0
source share
1 answer

See the files.insert example for the basics. In this example, it uses multipart, but the transition to resume is quite simple. Basically, you need to change the uploadType parameter to "renewable". The result from the insert / update will contain a link to the loader, than you can use to send content / check for completion.

 media = Google::APIClient::UploadIO.new(file_name, mime_type) result = client.execute( :api_method => drive.files.insert, :body_object => file, :media => media, :parameters => { 'uploadType' => 'resumable', 'alt' => 'json'}) # Send content result.resumable_upload.send_all(client) 

In the upcoming beta version of the client library (any day now :) this will change a bit, so the download will work a little more evenly, regardless of the protocol. In most cases, moving forward, calling execute () will suffice, and it will try to load the file. But the existing method will still work. You can also return by simply calling:

 if !result.resumable_upload.complete? client.execute(result.resumable_upload) # Continue sending... end 

The example does not display error handling, but can you check result.resumable_upload.complete? or .expired? to check the status. Incomplete renewable downloads expire after a period of inactivity (of the order of an hour or so). If expired, you will need to restart from the beginning.

+3
source

Source: https://habr.com/ru/post/1481050/


All Articles