Annotated examples written in Ruby.
Checked (✅︎) topics are completed. Topics without a check still need to be added.
- Google Drive Examples
See the Getting started section in the project README.md
See Obtaining an authenticated DriveService section in the project README.md
Each HTTP connection that your client makes results in a certain amount of overhead. The Google Drive API supports batching, to allow your client to put several API calls into a single HTTP request.
See examples/drive_service_batch for an example of how to batch Drive API.
You're limited to 100 calls in a single batch request. If you need to make more calls than that, use multiple batch requests.
Google Drive organizes files in collections, describes files by types, and provides specific attributes for each file to facilitate file manipulation.
The Google Drive API represents files stored on Drive as a File resource.
See File & folders overview in the Google Drive API documentation for more information.
Folders are treated as a type of file. For more details about folders, see File types.
examples/file_search shows how to list all the files for the authenticated user.
This example illustrates how to:
- Use a search query to filter results with specific examples for:
- Omitting files that are in the trash
- Only returning files in a specific folder
- Specify which data to return about each file
- How to retrieve the name of the containing folder for each file
- How to retrieve pagenated results
examples/file_get shows how to get a File from the drive and controlling which fields are returned.
Using the drive_service.create_file
with no parameters creates an empty data file
with the following attributes:
kind
is set to "drive.file",id
is set to a unique idname
is set to "Untitled"mime_type
of application/octet-stream.
The mime_type
parameter defines the type of file to create or to create a folder.
See Google Workspace & Google Drive supported MIME types
for a list of supported MIME types.
Google Apps files (document, spreadsheet, or presentation) may be created by specifying
their specific mime-type
:
- Google Docs: application/vnd.google-apps.document
- Google Sheets: application/vnd.google-apps.spreadsheet
- Google Slides: application/vnd.google-apps.presentation
- See Google Workspace & Google Drive supported MIME types for a complete list.
See Create and populate folders
for more about creating a folder. A folder is a file with a mime_type
of
"application/vnd.google-apps.folder".
Use the :parents
parameter to give the id of the folder that should contain the
file. Omitting this parameter or passing an empty array will place the new file
in the user's My Drive
root folder.
examples/file_create shows how to create a file specifying name, mime-type, parent, and initial data.
Use drive_service.update_file
to upload file content.
The Drive API does not allow for partial modifications or appending data directly on the server. To achieve this, the entire file must first be downloaded from the server. After downloading, any alterations or additions are made locally. Once these modifications are complete, the updated file must be uploaded in its entirety back to the server, effectively replacing the original file.
This approach ensures data integrity, but it might not be bandwidth-efficient, especially for large files.
examples/file_upload_content shows how to overwrite a file's content.
Use drive_service.get_file
to download file content.
examples/file_download_content shows how to download a file's content.
To export Google Workspace document, use the files.export
method with
the ID of the file to export and the correct MIME type. Exported content is limited
to 10 MB.
See Export MIME types for Google Workspace documents for a list of possible MIME types for each document type.
See examples/file_export_spreasheet for an example of how to export a spreadsheet to a PDF.
To send a file to the trash, use drive_service.update_file
to set the trashed
file attribute to true
.
examples/file_send_to_trash shows how to send a file to the trash.
To recover a file from the trash, use drive_service.update_file
to set the trashed
file attribute to false
.
examples/file_recover_from_trash shows how to recover a file from the trash.
Use drive_service.delete_file
to delete a file without sending it to the trash.
examples/file_delete shows how to delete a file.
Every Google Drive file, folder, and shared drive have associated
permission
resources. Each resource identifies the permission for a specific type
(user, group,
domain, anyone) and role
, such as "commenter" or "reader." For example, a file might
have a permission granting a specific user (type=user
) read-only access (role=reader
)
while another permission grants members of a specific group (type=group
) the ability
to add comments to a file (role=commenter
).
For a complete list of roles and the operations permitted by each, refer to [Roles & permissions-(https://developers.google.com/drive/api/guides/ref-roles).
Use drive_service.create_permission
to create a permission on a file.
examples/permission_create shows how to create a permission for a file.
Use drive_service.list_permissions
to list the permissions on a file.
examples/permission_list shows how to list the permissions for a file.
Use drive_service.update_permission
to list the permissions on a file.
examples/permission_update shows how to update a permissions for a file.
Use drive_service.delete_permission
to delete a permission for a file.
examples/permission_delete shows how to delete a permissions for a file.