GoogleDriveFilesystem
Stores files in a single Google Drive folder. Each directory maps to a Drive folder under the configured root, and paths use POSIX semantics (for example /notes/todo.txt).
For interface details, see WorkspaceFilesystem Interface.
InstallationDirect link to Installation
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/google-drive
pnpm add @mastra/google-drive
yarn add @mastra/google-drive
bun add @mastra/google-drive
UsageDirect link to Usage
Add a GoogleDriveFilesystem to a workspace and assign it to an agent:
import { Agent } from '@mastra/core/agent'
import { Workspace } from '@mastra/core/workspace'
import { GoogleDriveFilesystem } from '@mastra/google-drive'
const workspace = new Workspace({
filesystem: new GoogleDriveFilesystem({
folderId: process.env.GOOGLE_DRIVE_FOLDER_ID!,
accessToken: process.env.GOOGLE_DRIVE_ACCESS_TOKEN!,
}),
})
const agent = new Agent({
id: 'drive-agent',
name: 'Drive Agent',
model: 'openai/gpt-4o-mini',
workspace,
})
AuthenticationDirect link to Authentication
Supply one of the following authentication options:
accessToken: A pre-obtained OAuth access token. Use thehttps://www.googleapis.com/auth/drivescope so the token can see folders shared with the authenticated identity.getAccessToken: A callback that returns a token. Useful when tokens are refreshed externally.serviceAccount: A Google service account. Share the target folder with the service account email.
Service accountDirect link to Service account
Service account auth is the recommended option for backend agents. There is no user consent flow and no token refresh handling. You only need two values from the service account JSON key file: client_email and private_key.
Set up the service accountDirect link to Set up the service account
- Open the Google Cloud Console and select or create a project.
- Go to APIs & Services > Library, search for Google Drive API, and select Enable.
- Go to APIs & Services > Credentials, select Create credentials > Service account, and complete the form. The role can be left blank — Drive permissions are granted by sharing folders, not by IAM roles.
- Open the new service account, go to the Keys tab, and select Add key > Create new key > JSON. The browser downloads a JSON key file.
- Copy the
client_emailvalue from the JSON file. This is the address you share Drive folders with.
Share the Drive folder with the service accountDirect link to Share the Drive folder with the service account
The service account is its own Google identity. It cannot see anything in Drive until you share with it explicitly.
- Open the target folder in Google Drive.
- Select Share.
- Paste the service account's
client_emailaddress. - Set the role to Editor (for read and write) or Viewer (for read-only access). Select Send.
- Copy the folder ID from the URL. It is the segment after
/folders/inhttps://drive.google.com/drive/folders/<folderId>.
Service accounts cannot create files in standard "My Drive" folders. A service account has no personal Drive storage quota, so any file it creates needs to be owned by a quota-bearing entity. If you only share a personal Drive folder, read operations work but writes fail with a quota error.
For write access, place the folder inside a shared drive (formerly Team Drive) and add the service account as a member of that shared drive. Shared drives provide the storage quota that service-account-created files need.
Read-only workloads against a personal Drive folder do not have this restriction.
Configure the filesystemDirect link to Configure the filesystem
Copy client_email and private_key from the JSON file into your environment:
GOOGLE_DRIVE_FOLDER_ID=1AbCdEfGhIjKlMnOpQrStUvWxYz
GOOGLE_DRIVE_CLIENT_EMAIL=my-bot@my-project.iam.gserviceaccount.com
# Wrap the value in quotes — the key contains newlines that must be preserved.
GOOGLE_DRIVE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkq...\n-----END PRIVATE KEY-----\n"
import { GoogleDriveFilesystem } from '@mastra/google-drive'
const filesystem = new GoogleDriveFilesystem({
folderId: process.env.GOOGLE_DRIVE_FOLDER_ID!,
serviceAccount: {
clientEmail: process.env.GOOGLE_DRIVE_CLIENT_EMAIL!,
privateKey: process.env.GOOGLE_DRIVE_PRIVATE_KEY!,
},
})
You do not need to copy the entire JSON file or pass other fields like project_id, client_id, private_key_id, or token_uri. They are not used. Only clientEmail and privateKey are required. privateKeyId, scopes, and subject are optional. scopes defaults to ['https://www.googleapis.com/auth/drive'], which is the scope required for the service account to see folders shared with it. The narrower drive.file scope only grants access to files the application created itself, so a folder shared with the service account would return 404 Not Found.
GoogleDriveFilesystem automatically normalizes the privateKey string before signing. It strips surrounding quotes (including escaped quotes from JSON-wrapped values), converts literal \n sequences to real newlines, normalizes \r\n line endings, and removes a trailing comma. The key works regardless of how your .env loader handles the value.
TroubleshootDirect link to Troubleshoot
404 File not found: <folderId>: The service account does not have access to the folder. Confirm the folder is shared with the exactclient_emailaddress and that the folder ID matches the URL.storageQuotaExceededon write: The folder is in a personal "My Drive". Move the folder to a shared drive and add the service account as a member.error:1E08010C:DECODER routines::unsupported: TheprivateKeyvalue is malformed. Confirm the value contains the full PEM block and that newlines are preserved (literal\nis fine).
Read-only modeDirect link to Read-only mode
Pass readOnly: true to block write operations (writeFile, appendFile, deleteFile, copyFile, moveFile, mkdir, rmdir).
const filesystem = new GoogleDriveFilesystem({
folderId,
accessToken,
readOnly: true,
})
Constructor parametersDirect link to Constructor parameters
folderId:
accessToken?:
getAccessToken?:
serviceAccount?:
id?:
readOnly?:
instructions?:
PropertiesDirect link to Properties
id:
name:
provider:
readOnly:
MethodsDirect link to Methods
GoogleDriveFilesystem implements the WorkspaceFilesystem interface, providing all standard filesystem methods:
readFile(path, options?)- Download file contentswriteFile(path, content, options?)- Upload or overwrite a fileappendFile(path, content)- Append content by reading and re-uploading the filedeleteFile(path, options?)- Delete a filecopyFile(src, dest, options?)- Copy a file using the Drivefiles.copyAPImoveFile(src, dest, options?)- Move a file between folders by swapping parentsmkdir(path, options?)- Create a folderrmdir(path, options?)- Remove a folderreaddir(path, options?)- List folder contents (supportsrecursiveandextensionfiltering)stat(path)- Return Drive metadata for a file or folderexists(path)- Check if a file or folder exists
NotesDirect link to Notes
- Google Drive allows multiple files with the same name in a folder.
GoogleDriveFilesystemresolves paths by picking the first match, so keep names unique within each folder when you rely on path-based lookup. writeFilecreates parent folders automatically whenrecursiveis unset (the default) ortrue. Setrecursive: falseto require the parent folder to already exist.expectedMtimeonWriteOptionsis honored — when the storedmodifiedTimediffers, the write is rejected withStaleFileErrorto support optimistic concurrency.- The provider only exercises Drive REST endpoints (
https://www.googleapis.com/drive/v3andhttps://www.googleapis.com/upload/drive/v3) through the built-infetch; no additional dependencies are required.