File Uploading to Google Drive - ruby

I am just starting with the Google Drive API. The quick start is quite handy, but it only explains file listing feature.
Cannot find the file upload feature in the Upload file data page.
The RI documentation for Google::Apis only contains hundreds of new line characters (1128 to be specific), and nothing interesting.
How do I upload files to google drive using the Ruby API?

Here are two upload examples using Drive V2 (one resumable, one not), and here you can see the method you should be using in V3 (create_file instead of insert_file), as well as an explanation of all the possible parameters you can provide.
The first example could be written in V3 like this:
drive_service.create_file({title: 'My Favorite Movie'}, upload_source: 'mymovie.m4v',
content_type: 'video/mp4')
Reference:
Class: Google::Apis::DriveV3
create_file

Related

FFMPEG using Google Drive API instead of Shared URL

We are using FFMPEG to stream a Google Drive URL into a node application.
Is there an FFMPEG method or library we can use to stream to FFMPEG using the Google Drive API instead of using the standard public shared URL?
At the moment using the URL works fine if the file size is <100mb but with bigger files we get an error:
https://drive.google.com/uc?export=download&id=fileId: Invalid data found when processing input
This is because we reach the pesky gDrive virus roadblock page:
From your question, I understood that your file is publicly shared. In this case, when the file size becomes large, the endpoint of https://drive.google.com/uc?export=download&id=fileId is required to be processed with 2 steps. Ref This has already been also mentioned in your question.
In this answer, in order to avoid this, I would like to propose to use the method of "Files: get" in Drive API and the API key. When Drive API and API key is used for the publicly shared file, no 2 step flow is required, and it can use it by changing only the URL.
Endpoint:
https://www.googleapis.com/drive/v3/files/{fileId}?alt=media&key={your API key}
For example, as a test, when you use curl command, you can use curl "https://www.googleapis.com/drive/v3/files/{fileId}?alt=media&key={your API key}".
References:
Download a file stored on Google Drive
Files: get
Using API keys

Google Drive API v3 : there isn't any way to get a download url for a google document?

The Google Drive API v2 to v3 migration guide says:
The exportLinks field has been removed from files. To export Google Documents, use the files.export method instead.
I don't want to export (download) the file right away. "files.export" will actually download the file. I want a link to download the file, later. This was possible in v2 by means of the exportLinks.
How can I in v3 accomplish the same? If it is not possible, why was this useful feature removed?
Besides, (similar problem to above) downloadUrl was also removed, and the suggested alternative ("files.get with ?alt=media") downloads the file instead of providing a download link. This means there is no way in v3 to get a public short lived URL for a file?
EDIT:
there is no way in v3 to get a public short lived URL for a file?
For regular files, apparently yes.
This seems to work fine (a public short lived link to the file with its right name and contents):
https://www.googleapis.com/drive/v3/files/ID?alt=media&access_token=TOKEN
For google apps files, no (not even private, as v2 exportLinks used to be).
https://www.googleapis.com/drive/v3/files/ID/exportmimeType=TYPEv&access_token=TOKEN
Similar to regular files, this URL is a short lived link to the file contents, but lacking of its right name.
BTW, I see the API is not behaving consistently: /drive/v3/files/FILEID delivers the right file name, but /drive/v3/files/FILEID/export does not.
I think the API itself should be setting the right Content-Disposition, as it is apparently doing when issuing a /drive/v3/files/FILEID call.
This file naming problem invalidates the workaround to the lack of ExportLinks in v3.
The v2 ExportLinks allowed me to link a file (which is not the same as getting its content right away). Anyone logged in and with the proper permissions was able to access it, and the link didn't needed any access_token, and it wasn't short lived. It was good and useful.
Building a link with a raw API call like /drive/v3/files/FILEID/export (with mandatory access_token) would be an close enough workaround (it is temporary and public, not the same as it was, anyway). However, the naming problem invalidates it.
In v2, regular files have a WebContentLink and google apps files have exportLinks. In v3 exportLinks are gone, and I don't see any suitable alternative to them.
Once you query for your file by id you can use the function getWebContentLink() to get the download link of the file (eg. $file->getWebContentLink() ).
I think you're placing too much emphasis on the word "method".
There is still a link to export a file, it's https://www.googleapis.com/drive/v3/files/fileIdxxxxx/export&mimeType=xxxxx/xxxxx. Make sure you URL encode the mime type.
Eg
https://www.googleapis.com/drive/v3/files/1fGBQ81haNU_nEiC5GITZD3bxT0ppL2LHg-C0ubD4Q_s/export?mimeType=text/csv&access_token=ya29.Gmo0BMvO-pVEPKsiD9j4D-NZVGE91MChRvwOcBSg3cTHt5uAClf-jFxcovQScbO2QQhwHS95eSGW1eQQcK5G1UQ6oI4BFEJJkntEBkgriZ14GbHuvpDL7LT2pKA--WiPuNoDDIuZMm5lWtlr
These links form part of the API, so the expectation is that you've written a client that sends authenticated requests, and deals with the response data. This explains why, if you simply paste the link into a browser without an access_token, it will fail. It also explains why the filename is export, ie. it isn't intended that your client would ever use a filename, but rather it should receive the data as a stream. This SO answer discusses the situation in more detail How to set name of file downloaded from browser?

Copy one slide from a google presentation into a new google presentation using API [duplicate]

I wish to use Google Drive API for creating a new Google Slides, but with one slide copied from another previous presentation.
My question is, is that possible to build a new Google Slides and insert slides (singularly) into the new one.
Or is it possible to delete the a slide from Google Presentation?
Although imperfect, you may be able to get close using the new Google Slides API. You can read the slide from the source presentation and then attempt to replicate it in the destination presentation. At the moment not all Slides features are supported in the API, so there may be some information loss is the process.
(Feb 2017) As of Nov 2016 with the release of the Google Slides API, an alternative to the solution the OP is asking for is now possible although not directly with the API. That workaround is that the ONE SLIDE you want copied is in its own standalone file. Then you can use two Google APIs to make it happen: the Slides API (to perform the document-level functions like adding new slides) as well as the Google Drive API (for file-oriented access like copying).
Sounds like you're using the Google APIs Client Library for Ruby, so you're set there. The next step is to create a project in the Google Developers Console with both the Google Drive and Google Slides APIs enabled, then download the client_secret*.json file after you create your OAuth2 credentials.
To help you get started, here's the Ruby quickstart sample for the Drive API, and here's the Ruby quickstart for the Slides API. If you're not "allergic" to Python (if you are, just pretend it's pseudocode ;) ), I've made several videos with more "real-world" examples of using the Slides API you can learn from and migrate to Ruby if desired.
Below is another Python example which you can port to Ruby that does exactly what the OP asked, assuming your template file is SLIDE_TEMPLATE_FILE, DRIVE is your Drive API service endpoint, and SLIDES is the endpoint for the Slides API. If someone PMs me a Ruby port, I'll update this answer with it.
# query for template file with one slide
TMPLFILE = SLIDE_TEMPLATE_FILE
rsp = DRIVE.files().list(q="name='%s'" % TMPLFILE).execute().get('files')[0]
# copy template file
DATA = {'name': 'Google Drive & Slides API template DEMO'}
DECK_ID = DRIVE.files().copy(body=DATA, fileId=rsp['id']).execute().get('id')
# create 2 more (1 title-only & 1 blank) slides in copied file
reqs = [
{'createSlide': {'slideLayoutReference': {'predefinedLayout': 'TITLE_ONLY'}}},
{'createSlide': {'slideLayoutReference': {'predefinedLayout': 'BLANK'}}},
]
SLIDES.presentations().batchUpdate(body={'requests': reqs},
presentationId=DECK_ID).execute()
Finally, yes, you can also delete slides from presentations with the deleteObject request, passing in the ID of the slide/page you want removed. Another workaround if you can't isolate the ONE SLIDE: copy the entire presentation & delete all pages except the ONE SLIDE, then start adding new slides. Hopefully the API will eventually get "the right solution" so we don't have to play these games.
It is not possible from the API to perform this complete operation from API.
Although, we can create new or copy existing Google Presentation.
Or, we can do that from browser interaction by ctrl+c one slide and ctrl+v into another one.

How can I get image list and download these images from public-shared-gallery-link by using Dropbox SDK?

I want to get the image list and download those images from a Dropbox's public-shared-link, which I get it from my client.
I'm using Dropbox SDK for ruby and only find the methods to manage files via my Dropbox account, such as put_file, get_file, get_file_and_metadata, get_chunked_uploader, upload and so on.
Is there any way to do that?
Dropbox API v1 has an endpoint that allows you to retrieve metadata about shared links:
https://www.dropbox.com/developers-v1/core/docs#metadata-link
Dropbox API v2 has a similar pair of endpoints that allows you to retrieve metadata and file content, respectively, from shared links:
https://www.dropbox.com/developers/documentation/http/documentation#sharing-get_shared_link_metadata
https://www.dropbox.com/developers/documentation/http/documentation#sharing-get_shared_link_file
Unfortunately, none of these are currently implemented in the Ruby SDK. You can call them directly though, or modify the Ruby SDK to do so.

Data in Mbox to JSON or CSV?

I just downloaded all my Gmail with the new download functionality from Google, and it gives me a large .mbox file. What would be a basic shell of a script to start extracting and processing individual emails from the file?
The book "Mining the Social Web" (O'Reilly, 2nd ed.) by Matthew Russell gives some code for doing this in Python. His code is all on github. You will want the files prefixed with 'mailbox'. https://github.com/ptwobrussell/Mining-the-Social-Web/tree/master/python_code
Check out this GitHub repo - https://github.com/PS1607/mbox-to-json
Also extracts the Attachments for you.
If you want to convert it into CSV instead, change line 55 in src/main.py from df.to_json to df.to_csv

Resources