Module Reference

Simple Google Drive API wrapper.

Examples

>>> from googledrive import Service
>>> with Service(credentials).files() as gdrive:
...   file_content = gdrive.read(('folderA', 'subfolder1'), 'filename')
...   gdrive.write(('folderB', 'subfolder2'), 'filename.txt',
...                file_content, 'text/plain')

Service

class googledrive.Service(credentials, *args)[source]

Simple wrapper class for Google Drive API.

SCOPE = 'https://www.googleapis.com/auth/drive'

OAuth scope string for Google Drive API.

files(max_retry: int = 3, retry_interval: float = 1)[source]

Return a Files object, a simple wrapper for files resource of Google Drive API.

Parameters
  • max_retry – The maximum number of retries for API calls.

  • retry_interval – The retry interval in seconds.

Files

class googledrive.Files(service, max_retry: int = 3, retry_interval: float = 1)[source]

Bases: object

Simple wrapper class for the files Resource of Google Drive API.

close() None[source]

Close API connection.

list(path: Optional[Union[str, List[str]]] = None, query: Optional[str] = None, fields: Optional[str] = None) List[Dict[str, Any]][source]

List or searches the files in a path.

Parameters
  • path – The path ID string, the list of path names, or None.

  • query – A query string for filtering the file results.

  • fields – The comma-separated list of the field paths to be included.

Returns

A list of files.

read(path: Union[str, List[str]], name: str) str[source]

Read the content of a file.

Parameters
  • path – The path ID string, or the list of path names.

  • name – The file name.

Returns

The file content as a string.

write(path: Union[str, List[str]], name: str, content: str, mimetype: str) str[source]

Write the content of a file.

The file is overwrote if it exists, or created otherwise.

Parameters
  • path – The path ID string, or the array of path names.

  • name – The file name.

  • content – The file content as a string.

  • mimetype – The mime-type of the file.

Returns

The file ID string.

each_files(parent_id: Optional[str] = None, query: Optional[str] = None, fields: Optional[str] = None) Iterator[Dict[str, Any]][source]

Iterate the files in a path.

Parameters
  • parent_id – The path ID string, or None.

  • query – A query string for filtering the file results.

  • fields – The comma-separated list of the field paths to be included.

Yields

Files.

get_path_id(path: List[str], root_id: str = 'root') str[source]

Get the file ID of the path.

Parameters
  • path – The array of path names.

  • root_id – The ID string of the root folder.

Returns

The ID string of the path.

get_id(parent_id: str, name: str) str[source]

Get the file ID.

Parameters
  • parent_id – The path ID string.

  • name – The file name.

Returns

The ID string of the file, or None unless the file exists.

create_file(parent_id: str, name: str, content: str, mimetype: str) str[source]

Create a file.

Parameters
  • parent_id – The path ID string.

  • name – The file name.

  • content – The file content as a string.

  • mimetype – The mime-type of the file.

Returns

The ID string of the created file, or None if it fails.

read_file_id(file_id: str) str[source]

Read the file content.

Parameters

file_id – The file ID string.

Returns

The file content as a string.

update_file_id(file_id: str, content: str, mimetype: str) None[source]

Update the file content.

Parameters
  • file_id – The file ID string.

  • content – The file content as a string.

  • mimetype – The mime-type of the file.

delete_file_id(file_id: str) None[source]

Delete a file.

Parameters

file_id – The file ID string.