Skip to content

Latest commit

 

History

History
294 lines (202 loc) · 11.4 KB

README.md

File metadata and controls

294 lines (202 loc) · 11.4 KB

Google Drive Examples

Annotated examples written in Ruby.

Checked (✅︎) topics are completed. Topics without a check still need to be added.

Getting started

✅︎ Creating a Google API service account

See the Getting started section in the project README.md

✅︎ Creating a DriveService instance

See Obtaining an authenticated DriveService section in the project README.md

✅︎ Batching DriveService requests

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.

Files and folders

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.

✅︎ Search for files

examples/file_search shows how to list all the files for the authenticated user.

This example illustrates how to:

  1. 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
  2. Specify which data to return about each file
  3. How to retrieve the name of the containing folder for each file
  4. How to retrieve pagenated results

✅︎ Get file

examples/file_get shows how to get a File from the drive and controlling which fields are returned.

✅︎ Create a file

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 id
  • name 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:

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.

✅︎ Upload file 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.

✅︎ Download file data

Use drive_service.get_file to download file content.

examples/file_download_content shows how to download a file's content.

✅︎ Export file

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.

✅︎ Send file to trash

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.

✅︎ Recover file from 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.

✅︎ Delete a file

Use drive_service.delete_file to delete a file without sending it to the trash.

examples/file_delete shows how to delete a file.

Share files, folders, and drives

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).

✅︎ Create permission

Use drive_service.create_permission to create a permission on a file.

examples/permission_create shows how to create a permission for a file.

✅︎ List permissions

Use drive_service.list_permissions to list the permissions on a file.

examples/permission_list shows how to list the permissions for a file.

✅︎ Update permission

Use drive_service.update_permission to list the permissions on a file.

examples/permission_update shows how to update a permissions for a file.

✅︎ Delete permission

Use drive_service.delete_permission to delete a permission for a file.

examples/permission_delete shows how to delete a permissions for a file.

Shortcuts

https://developers.google.com/drive/api/guides/shortcuts

Create a shortcut to a file

Search for shortcuts

Other

File revisions

Store application-specific data

Manage file metadata

Manage comments and replies

Add custom file properties

Create a shortcut to a Drive file

Create a shortcut to app content

Protect file content from modification

Access link-shared files using resource keys

Handle changes

Identify which change log to track

Track changes for users and shared drives

Retrieve changes

Receive notifications for resource changes

Manage labels

Manage labels

Set label field

Unset label field

Remove label

List labels from file

Return specific labels from a file

Search by label or field